10

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

4 回答 4

11

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

于 2013-05-02T21:09:13.533 回答
4

这个答案中提取代码:

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 的解决方案都要快。它也更简单一些。

于 2019-02-01T21:51:16.247 回答
-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 的包装器

备注

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

于 2013-05-02T19:33:43.350 回答
-2

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

于 2013-05-02T19:40:54.113 回答