0
我試圖在Windows Phone 8中使用WriteableBitmap更改圖像的顏色。基本上,我有一個帶有黑色和透明背景的圖標(png)。我試圖把它與透明背景轉換爲白色如下:更改顏色時WritableBitmap問題
StreamResourceInfo sri = Application.GetResourceStream(new Uri(value.ToString(), UriKind.Relative));
BitmapImage src = new BitmapImage();
src.SetSource(sri.Stream);
// Get WriteableBitmap
WriteableBitmap bitmap = new WriteableBitmap(src);
// Iterate through each pixel.
for (int x = 0; x < bitmap.Pixels.Length; x++)
{
byte[] actualColorValues = BitConverter.GetBytes(bitmap.Pixels[x]);
byte[] modifiedColorValues = new byte[4];
modifiedColorValues[0] = 255;
modifiedColorValues[1] = 255;
modifiedColorValues[2] = 255;
//opacity
modifiedColorValues[3] = actualColorValues[3];
bitmap.Pixels[x] = BitConverter.ToInt32(modifiedColorValues, 0);
}
// Set Image object, defined in XAML, to the modified bitmap.
return bitmap;
將圖像轉換爲白色,但它也會稍有失真特別的邊緣,這是不完美尤其是在邊緣的實際圖標。這是一個已知的問題,還是我錯過了什麼?
您可以發佈與示例圖像的一個很好的例子herè? – 2013-05-05 07:17:01
您可以使用矩形更改顏色:將所需顏色設置爲屬性填充並將ImageBrush與源代碼設置爲屬性OpacityMask。 – Catherine 2013-06-10 14:13:47