简体   繁体   中英

WritableBitmap issue when changing the color

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?

There is a good example her e 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM