删除图像透明度 [英] Remove image transparency

查看:83
本文介绍了删除图像透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在大学里正在研究机器学习问题,我的第一项工作是将图像转换为黑白图像.

I'm working on a Machine Learning problem at college and my first job is to convert images into black and white.

问题是我的图像具有透明度,我不知道如何删除它.

The problem is that my image has transparency, and I don't know how to remove it.

我正在尝试:

public static Bitmap RemoveTransparency (Bitmap src)
{            
    Bitmap   target = new Bitmap (src.Size.Width, src.Size.Height);
    Graphics g      = Graphics.FromImage (target);

    g.Clear (Color.White);            
    g.DrawImage (src, 0, 0);

    return target;
}

输入图像的示例:

"RemoveTransparency"调用后的输出图像示例:

An example of output image after "RemoveTransparency" call:

有人知道发生了什么吗?好像背景和字母具有相同的颜色...如果我将颜色着色为白色,我的背景是黑色的吗?

Does anyone know what's going on? Seems like background an the letters have the same color... and my my background is black if I'm coloring to white?

谢谢!

推荐答案

您需要先将Graphics对象的CompositingMode设置为SourceOver,然后再在其上绘制其他图像.

You need to set the CompositingMode of your Graphics object to SourceOver before drawing the other image on top of it.

g.Clear(Color.White);
g.CompositingMode = CompositingMode.SourceOver;
g.DrawImage(src, 0, 0);

默认的CompositingModeSourceCopy,这将获取src图像中的透明黑色(R = G = B = A = 0)像素,并将其渲染为黑色像素. SourceOver将进行alpha混合,这就是您想要的.

The default CompositingMode is SourceCopy, which is taking the transparent black (R=G=B=A=0) pixels in your src image and rendering them as black pixels. SourceOver will do alpha blending, which is what you're looking for.

有关详细信息,请参见此处:

See here for details: CompositingMode Enumeration

这篇关于删除图像透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆