2013-05-05 77 views
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; 

將圖像轉換爲白色,但它也會稍有失真特別的邊緣,這是不完美尤其是在邊緣的實際圖標。這是一個已知的問題,還是我錯過了什麼?

+1

您可以發佈與示例圖像的一個很好的例子herè? – 2013-05-05 07:17:01

+0

您可以使用矩形更改顏色:將所需顏色設置爲屬性填充並將ImageBrush與源代碼設置爲屬性OpacityMask。 – Catherine 2013-06-10 14:13:47

回答

0

有關於如何改變像素顏色

我也用這樣的方法來獲得一個int相類似的色彩

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

    return argb; 
}