删除位图的黑色背景颜色

我需要在C#VS2013中删除位图的黑色背景颜色。

就像我在canvas上画一些点一样。 canvas是黑色的,我只需要将canvas更改为透明,同时在其上保留彩色点而不做任何更改。

我得到了解决方案: 如何从Bitmap中删除白色背景颜色

Bitmap capcha = new Bitmap(@"C:/image.png"); capcha.MakeTransparent(Color.Black); 

但是,背景仍有灰色,如覆盖图像上的点的雾。

如何删除它?

更新我使用的代码:

 ImageAttribute imageAttribute = new ImageAttribute(); imageAttribute.SetGamma(0.5f, ColorAdjustType.Bitmap); gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imageAttribute ); 

我有同样的事情。

更多C#代码更新以绘制图像:

  System.Drawing.Bitmap canvasImage = new System.Drawing.Bitmap(xSize, ySize, System.Drawing.Imaging.PixelFormat.Format32bppArgb); canvasImage.MakeTransparent(Color.Black); Graphics g = Graphics.FromImage(canvasImage); System.Drawing.Bitmap tempImage = myDrawImageFunction(myPoints); g.Clear(Color.Transparent); // this only give me an empty image without any points on it. But, if I uncomment it, I will have an image with black background. // my understanding about g.DrawImage() is to draw points on tempImage // after clearing the background. But, the image still have a foggy covering on the image. g.DrawImage(tempImage, new System.Drawing.PointF(x_position, y_position)); 

我希望在绘制任何点之前为“tempImage”提供透明背景。

示例图像具有需要移除的背景,但图像上的彩色点需要保持不变。

需要移除黑色背景但需要保留彩色点的图像。

这将完成工作:

 public Color MakeTransparent(Color c, int threshold) { // calculate the weighed brightness: byte val = (byte)((cR * 0.299f + cG * 0.587f + cB * 0.114f)); return val < threshold ? Color.FromArgb(0, cR, cG, cB) : c; } 

您可以在像素上使用双循环,但为了获得快速结果,您应该从使用LockBits post (第二部分!)中的代码中调用它。

改变这个

 ModifyHue hueChanger = new ModifyHue(MaxChannel); 

到新function:

 ModifyHue hueChanger = new ModifyHue(MakeTransparent); 

并用合适的阈值调用它,可能是10或20 ..:

 c = hueChanger(c, 20); 

该函数跳过调用系统的MakeTransparent函数,并直接将每个像素的alpha通道设置为0。

如果你想创建一个统一的颜色而不是透明的颜色,它应该很容易修改(例如通过返回Color.FromArgb(255, 0, 0, 0)为纯黑色)

请注意,虽然链接post中的代码同时采用24和32 bbp格式,但你绝对应该保存为JPG,因为这将重新引入工件 ,结果将无法与例如TransparencyKey颜色一起使用。

而是像汉斯建议的那样将它保存为PNG!

我希望你可以修改按钮代码到一个function:-)