0

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

dtlvd
  • 457
  • 1
  • 8
  • 21
Rachel
  • 133
  • 1
  • 16
  • Please anyone help me out here. – Rachel Apr 04 '18 at 07:10
  • The grey shaded pixels on the edge are changed to white. I think it is why you have this problem. You may try to apply another rule for those pixels like not changing the color for a certain percentage of grey. – dtlvd Apr 04 '18 at 07:54
  • I tried modifying the code as per your suggestion but its not working – Rachel Apr 04 '18 at 12:27
  • are you still getting the same behaviour or do you have some improvements ? – dtlvd Apr 04 '18 at 15:37
  • same behaviour. – Rachel Apr 05 '18 at 05:50
  • 2
    I finally found the solution in this link https://stackoverflow.com/questions/16382536/pixel-by-pixel-color-conversion-writeablebitmap-png-black-only-to-color-with?rq=1 – Rachel Apr 05 '18 at 11:17

0 Answers0