仅将图像的一种颜色复制到另一图像

时间:2012-02-08 15:52:52

标签: c# image graphics

我需要将像素从一个图像复制到另一个图像,但只复制黑色像素。我必须将黑色像素分开,以便使用打印机色带上的K面板发送到打印机。

基本上我需要让图像中的每个像素都不是黑色透明或白色。

最好的方法是什么?

我唯一的想法就是:

        var attr = new ImageAttributes();
        attr.SetColorKey(minColor, maxColor);
        using (Graphics g = Graphics.FromImage(backGround))
        {
            var destRect = new Rectangle(0, 0, backGround.Width, backGround.Height);

            g.DrawImage(kPanelImage, destRect, 0, 0, backGround.Width, backGround.Height, GraphicsUnit.Pixel, attr);
        }

2 个答案:

答案 0 :(得分:2)

Bitmap mySource = new Bitmap("your_image.jpg");

for(int w=0; w<mySource.Width; ++w)
   for(int h=0; h<mySource.Height; ++h)
   {
      Color pixelColor = mySource .GetPixel(w, h);
      if ( pixelColor != Color.Black )
           mySource .SetPixel(w, h, Color.White);
   }

答案 1 :(得分:1)

为什么不迭代图像中的每个像素,然后检查它的颜色。如果它不是黑色,则将其颜色更改为白色