你能在Bitmap图像中将一种颜色更改为另一种颜色吗?

时间:2013-05-02 19:29:44

标签: c# colors bitmap

对于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);

4 个答案:

答案 0 :(得分:10)

通过对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);

调用

答案 1 :(得分:0)

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的任何解决方案都要快。这也更简单了。

答案 2 :(得分:-1)

您可以使用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可能会返回不同的值   因为四舍五入。此外,如果您在位图上调用GdipBitmapSetPixel   对象的颜色深度为每像素16位,信息可以是   在从32位到16位的转换期间丢失,以及随后的呼叫   到GdipBitmapGetPixel可能会返回不同的值。

答案 3 :(得分:-1)

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