我正在开发一个Windows应用程序。
我想将PNG的像素颜色从黑色变为白色.PNG具有透明背景。
为此,我写了一个小功能,看起来像这样:
public void colorImageChange()
{
byte a = 255;
StreamResourceInfo sri = Application.GetResourceStream(new Uri("Assets/Picto_1604_Panther_Black.png", UriKind.Relative));
BitmapImage src = new BitmapImage();
src.SetSource(sri.Stream);
// Get WriteableBitmap
WriteableBitmap bitmap = new WriteableBitmap(src);
for (int x = 0; x < bitmap.Pixels.Length; x++)
{
byte[] actualColorValues = BitConverter.GetBytes(bitmap.Pixels[x]);
byte[] modifiedColorValues = new byte[4];
modifiedColorValues[0] = a;
modifiedColorValues[1] = a;
modifiedColorValues[2] = a;
//modifiedColorValues[3] = a;
//opacity
modifiedColorValues[3] = actualColorValues[3];
bitmap.Pixels[x] = BitConverter.ToInt32(modifiedColorValues, 0);
}
// Set Image object, defined in XAML, to the modified bitmap.
ImageViewer1.Source = bitmap;
}
我能够改变图像的颜色,但我的问题是,图像的边缘不平滑。 我附加了预期的图像(左侧)和输出图像(右侧)。