C# – Convert transparent png in color to single color

bitmapctransparency

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.

Best Answer

If the image doesn't use alpha channel for transparency then the following will do:

Bitmap image;

for (int x = 0; x < image.Width; x++)
{
    for (int y = 0; y < image.Height; y++)
    {
        if (image.GetPixel(x, y) != Color.Transparent)
        {
            image.SetPixel(x, y, Color.White);
        }
    }
}