我试图在windows phone 8中使用WriteableBitmap改变图像的颜色。基本上,我有一个图标(png)与黑色和透明的背景。我已经尝试将其转换为透明背景的白色,如下所示:
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;
图像转换为白色,但它稍微扭曲,特别是边缘,它不是完美的,特别是在边缘作为实际的图标。这是已知的问题,还是我遗漏了什么?
这里有一个关于如何更改像素颜色的好例子
我也会使用这样的方法来获得color
的int等效物public static int GetArgbValues(Color c)
{
string colorcode = c.ToString();
int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
return argb;
}