Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 881
jesperlind Avatar asked Aug 01 '10 16:08

jesperlind


People also ask

Can you change transparency on PNG?

Just import your PNG image in the editor on the left, set the desired opacity level in the options, and you'll instantly get a semi-transparent PNG on the right. Free, quick, and very powerful. Import a PNG – change its opacity.

How do I change the color of transparency?

To make a color transparent in Photoshop, go to Select > Color Range. Click on the color you want to remove, then hold Shift to add additional colors to the selection. Adjust the Fuzziness slider until your subject is white then click OK. Lastly, press Delete to make the selected colors transparent.

Can PNG have partial transparency?

GIF and PNG‑8 formats support one level of transparency—pixels can be fully transparent or fully opaque, but not partially transparent.


1 Answers

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);
        }
    }
}
like image 58
Cloudanger Avatar answered Sep 21 '22 10:09

Cloudanger