我有一张JPEG格式的图片,白色背景和黑色圆圈。
如何将此图像转换为PNG格式,白色背景将是透明的,黑色仍然存在?
我也是程序员,如果C#代码中有一些想法,我会非常高兴。此外,我正在寻找转换器,工具,程序。
谢谢。
杰夫
答案 0 :(得分:9)
这是有效的,但解决方案很慢。您可以使用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);
}
答案 1 :(得分:1)
您可以使用ImageMagick工具like this example。
您需要将-background
选项设置为transparent
,将-alpha
选项设置为set
并使用-transparent
选项设置颜色你希望被解释为透明的。另请参阅convert tool reference。