WritableBitmap issue when changing the color

1.1k Views Asked by At

I am trying to change the color of an image using WriteableBitmap in windows phone 8. Basically, I have an icon (png) with black color and transparent background. I have tried to convert it to white color with transparent background as follows:

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;

The image converts to white but it is slightly distorted especially the edges and it is not perfect especially at the edges as the actual icon. Is that a known issue, or am I missing something?

1

There are 1 best solutions below

0
On

There is a good example here on how to change pixel colors

I would also use a method like this to get an int equivalent of color

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

    return argb;
}