【问题标题】:WritableBitmap issue when changing the color更改颜色时的 WriteableBitmap 问题
【发布时间】:2014-01-08 12:24:22
【问题描述】:

我正在尝试在 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;

图像转换为白色,但略微失真,尤其是边缘,并且作为实际图标的边缘并不完美,尤其是边缘。这是一个已知问题,还是我遗漏了什么?

【问题讨论】:

  • 你能发布示例图片吗?
  • 您可以使用 Rectangle 更改颜色:将所需颜色设置为属性 Fill 并将 ImageBrush 与源设置为您的图像到属性 OpacityMask。

标签: c# xaml windows-phone-8 writablebitmap


【解决方案1】:

有一个很好的例子here 关于如何改变像素颜色

我也会使用这样的方法来获得颜色的 int 等价物

public static int GetArgbValues(Color c)
{
    string colorcode = c.ToString();
    int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);

    return argb;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 2020-08-05
    • 1970-01-01
    相关资源
    最近更新 更多