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

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

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

回答1:

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); }


回答2:

You could use the ImageMagick tool like this example.

You will need to set the -background option to transparent, set the the -alpha option to set and use the -transparent option to set the colour you want to be interpretted as transparent. See also the convert tool reference.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!