【问题标题】:Can you change one colour to another in an Bitmap image?您可以将位图图像中的一种颜色更改为另一种颜色吗?
【发布时间】:2013-04-27 02:34:49
【问题描述】:

对于Bitmap,有一个MakeTransparent 方法,有没有类似的方法可以将一种颜色更改为另一种颜色?

// This sets Color.White to transparent
Bitmap myBitmap = new Bitmap(sr.Stream);
myBitmap.MakeTransparent(System.Drawing.Color.White);

有什么东西可以做这样的事情吗?

Bitmap myBitmap = new Bitmap(sr.Stream);
myBitmap.ChangeColor(System.Drawing.Color.Black, System.Drawing.Color.Gray);

【问题讨论】:

    标签: c# colors bitmap


    【解决方案1】:

    出于对 Yorye Nathan 评论的好奇,这是我通过修改 http://msdn.microsoft.com/en-GB/library/ms229672(v=vs.90).aspx 创建的扩展。

    它可以将位图中的所有像素从一种颜色转换为另一种颜色。

    public static class BitmapExt
    {
        public static void ChangeColour(this Bitmap bmp, byte inColourR, byte inColourG, byte inColourB, byte outColourR, byte outColourG, byte outColourB)
        {
            // Specify a pixel format.
            PixelFormat pxf = PixelFormat.Format24bppRgb;
    
            // Lock the bitmap's bits.
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmpData =
            bmp.LockBits(rect, ImageLockMode.ReadWrite,
                         pxf);
    
            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;
    
            // Declare an array to hold the bytes of the bitmap. 
            // int numBytes = bmp.Width * bmp.Height * 3; 
            int numBytes = bmpData.Stride * bmp.Height;
            byte[] rgbValues = new byte[numBytes];
    
            // Copy the RGB values into the array.
            Marshal.Copy(ptr, rgbValues, 0, numBytes);
    
            // Manipulate the bitmap
            for (int counter = 0; counter < rgbValues.Length; counter += 3)
            {
                if (rgbValues[counter] == inColourR &&
                    rgbValues[counter + 1] == inColourG &&
                    rgbValues[counter + 2] == inColourB)
    
                 {
                    rgbValues[counter] = outColourR;
                    rgbValues[counter + 1] = outColourG;
                    rgbValues[counter + 2] = outColourB;
                 }
            }
    
            // Copy the RGB values back to the bitmap
            Marshal.Copy(rgbValues, 0, ptr, numBytes);
    
            // Unlock the bits.
            bmp.UnlockBits(bmpData);
        }
    }
    

    bmp.ChangeColour(0,128,0,0,0,0);调用

    【讨论】:

    • 谢谢。帮助过我。您还可以修改代码以使用透明 PNG。只需将像素格式更改为 PixelFormat.Format32bppArgb 并将 counter 增加 4 而不是 3。我需要这样做,否则代码会弄乱透明背景。
    • 谢谢。非常有用。但是红色和蓝色位是相反的。 ;-)
    • 制作 args Color 对象可能会更加用户友好;)
    • 请注意,您应该真的从图像中获取像素格式并检查,而不是仅仅假设它.
    • 另一个注意事项:这忽略了遍历字节时的步幅。馊主意。逐行遍历图像,否则如果您的步幅与一行像素的确切数据长度不匹配,您的操作将在第一行之后发生变化。步幅通常与四个字节的倍数对齐。
    【解决方案2】:

    this answer提取代码:

    public static class BitmapExtensions
    {
        public static Bitmap ChangeColor(this Bitmap image, Color fromColor, Color toColor)
        {
            ImageAttributes attributes = new ImageAttributes();
            attributes.SetRemapTable(new ColorMap[]
            {
                new ColorMap()
                {
                    OldColor = fromColor,
                    NewColor = toColor,
                }
            }, ColorAdjustType.Bitmap);
    
            using (Graphics g = Graphics.FromImage(image))
            {
                g.DrawImage(
                    image,
                    new Rectangle(Point.Empty, image.Size),
                    0, 0, image.Width, image.Height,
                    GraphicsUnit.Pixel,
                    attributes);
            }
    
            return image;
        }
    }
    

    虽然我没有对其进行基准测试,但这应该比任何在循环中执行 GetPixel/SetPixel 的解决方案都要快。它也更简单一些。

    【讨论】:

      【解决方案3】:

      您可以为此使用SetPixel

      private void ChangeColor(Bitmap s, System.Drawing.Color source, System.Drawing.Color target)
      {
          for (int x = 0; x < s.Width; x++)
          {
              for (int y = 0; y < s.Height; y++)
              {
                  if (s.GetPixel(x, y) == source)
                      s.SetPixel(x, y, target);
              }                
          }
      }
      

      GetPixel 和 SetPixel 是 gdiplus.dll 函数 GdipBitmapGetPixel 和 GdipBitmapSetPixel 的封装

      Remarks:

      根据位图的格式,GdipBitmapGetPixel 可能不会 返回与 GdipBitmapSetPixel 设置的值相同的值。例如, 如果您在其像素格式的 Bitmap 对象上调用 GdipBitmapSetPixel 是 32bppPARGB,像素的 RGB 分量是预乘的。一种 对 GdipBitmapGetPixel 的后续调用可能会返回不同的值 因为四舍五入。此外,如果您在 Bitmap 上调用 GdipBitmapSetPixel 颜色深度为每像素 16 位的对象,信息可以是 在从 32 位到 16 位的转换过程中丢失,以及随后的调用 到 GdipBitmapGetPixel 可能会返回不同的值。

      【讨论】:

      • 效率低下,完全可以作为答案。
      • 所以,如果我有一张照片,我必须得到GetPixelSetPixel n 次,其中 n 是像素数。因此,对于常规图像,甚至是徽标,这可能是 n = 150k+...
      • 如果语言中已经存在为此的方法,它不会在后台进行相同的迭代吗? ...
      【解决方案4】:

      您需要一个库,该库提供了一种无需使用像素即可修改图像色彩空间的方法。 LeadTools 有一个非常广泛的图像库,您可以使用它来支持色彩空间修改,including swapping colors

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-19
        • 1970-01-01
        • 1970-01-01
        • 2021-06-26
        • 2012-08-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多