Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a JPEG image to a PNG one with transparent background?

Tags:

c#

image

png

jpeg

I have a JPEG format image, with a white background and a black circle.

How can I transform this image to a PNG format that the white background will be transparent and the black remains there?

I'm a programmer too, and if there are some ideas in C# code I will be very happy. Also I'm looking for a converter, tool, program anything.

Thank you.

Jeff

like image 966
Jeff Norman Avatar asked Dec 22 '22 22:12

Jeff Norman


1 Answers

Here is working, but slow solution. You can speed it up by using Bitmap.LockBits().

using (Image img = Image.FromFile(filename))
using (Bitmap bmp = new Bitmap(img))
{
    for (int x = 0; x < img.Width; x++)
    {
        for (int y = 0; y < img.Height; y++)
        {
            Color c = bmp.GetPixel(x, y);
            if (c.R == 255 && c.G == 255 && c.B == 255)
                bmp.SetPixel(x, y, Color.FromArgb(0));
        }
    }
    bmp.Save("out.png", ImageFormat.Png);
}
like image 133
ironic Avatar answered Dec 24 '22 13:12

ironic