Pixel by Pixel color conversion using WriteableBitmap

351 Views Asked by At

I am working on a windows application.

I want to do a pixel by pixel color change of a PNG from black to white.PNG has a transparent background.

For this purpose, I have written a little function, that looks like this:

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;
}

I am able to change the color of image but my Problem is, that the edges of image are not smooth. I am attaching the expected image (to left) and the output image (to right).

enter image description here enter image description here

0

There are 0 best solutions below