Search code examples
c#bitmaptransparency

Convert transparent png in color to single color


I am working with Bitmap C# and wondering how to convert a color png image to only one color. I want all the visible colors in the image to become white. The parts that are transparent should remain transparent. I am going to display these agains a grey background.


Solution

  • The other answers was helpful and got me going, thanks a lot. I couldn't make them work though, not sure why. But I also found out that I wanted to keep the original alpha value of the pixels, rendering the edges smooth. This is what I came up with.

    for (int x = 0; x < bitmap.Width; x++)
    {
        for (int y = 0; y < bitmap.Height; y++)
        {
            Color bitColor = bitmap.GetPixel(x, y);
            //Sets all the pixels to white but with the original alpha value
            bitmap.SetPixel(x, y, Color.FromArgb(bitColor.A, 255, 255, 255));
        }
    }
    

    Here is a screen dump of the result magnified a few times (original on top): alt text
    (source: codeodyssey.se)