一种制造图像的每个像素具有特定颜色的透明像素、图像、透明、颜色

由网友(知足,才能常乐)分享简介:我有型System.Drawing.Image对象的对象,希望让每一个像素有一些特定的颜色,例如黑色,透明(即设置阿尔法为0,该像素)。I have an object of the type System.Drawing.Image and want to make every pixel which has so...

我有型System.Drawing.Image对象的对象,希望让每一个像素有一些特定的颜色,例如黑色,透明(即设置阿尔法为0,该像素)。

I have an object of the type System.Drawing.Image and want to make every pixel which has some specific color, for example black, transparent (that is, set alpha to 0 for this pixel).

什么是做到这一点的最好方法是什么?

What is the best way to do this?

推荐答案

一个很好的方法是使用 ImageAttributes 类设置的颜色列表来绘制重新映射时发生。这样做的好处是不错的表现,以及让您可以非常容易地改变重映射颜色。尝试像这样code ...

One good approach is to use the ImageAttributes class to setup a list of colors to remap when drawing takes place. The advantage of this is good performance as well as allowing you to alter the remapping colors very easily. Try something like this code...

ImageAttributes attribs = new ImageAttributes();
List<ColorMap> colorMaps = new List<ColorMap>();
//
// Remap black top be transparent
ColorMap remap = new ColorMap();
remap.OldColor = Color.Black;
remap.NewColor = Color.Transparent;
colorMaps.Add(remap);
//
// ...add additional remapping entries here...
//
attribs.SetRemapTable(colorMaps.ToArray(), ColorAdjustType.Bitmap);
context.Graphics.DrawImage(image, imageRect, 0, 0, 
                           imageRect.Width, imageRect.Height, 
                           GraphicsUnit.Pixel, attribs);
阅读全文

相关推荐

最新文章