首页 > 解决方案 > 绘制 8bpp 位图时如何使调色板中的颜色透明

问题描述

我有一个带有自定义 256 调色板的 8bpp 索引位图,其中调色板(Color.PinkColor.Green)中的特定颜色表示透明度。
我可以在位图上使用该MakeTransparent(color)方法(每种颜色两次),但它将其转换为 32bpp。所以我使用的是:

using var imageAttr = new ImageAttributes();
imageAttr.SetColorKey(pink, pink, ColorAdjustType.Default);

接着

g.DrawImage(bitmap, destRect, X, Y, Width, Height, GraphicsUnit.Pixel, imageAttr);

它按应有的方式绘制位图,但仅对Color.Pink透明颜色进行更改。我怎样才能为第二种颜色(Color.Green)做到这一点?

标签: c#winformsbitmaptransparencygdi+

解决方案


当您需要重新映射位图调色板的索引颜色时, ImageAttributes.SetColorKey()和Bitmap.MakeTransparent都不是首选:前者一次只能设置一种颜色,后者将原始图像转换为 32bpp 图像。

您需要更改索引图像调色板或使用ImageAttributes.SetRemapTable()方法绘制新位图。此方法接受一组ColorMap对象。ColorMap 用于指定在绘制位图时替换旧颜色的新颜色。


让我们制作一个示例 8bpp 图像并应用部分调色板,然后使用Image.LockBits解析BitmapData并将这些颜色应用于一组 3x3 矩形:

var image = new Bitmap(12, 12, PixelFormat.Format8bppIndexed);
var palette = image.Palette;  // Copy the Palette entries
palette.Entries[0] = Color.SteelBlue;
palette.Entries[1] = Color.Pink;
palette.Entries[2] = Color.Red;
palette.Entries[3] = Color.Orange;
palette.Entries[4] = Color.YellowGreen;
palette.Entries[5] = Color.Khaki;
palette.Entries[6] = Color.Green;
palette.Entries[7] = Color.LightCoral;
palette.Entries[8] = Color.Maroon;
image.Palette = palette;  // Sets back the modified palette

var data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.WriteOnly, image.PixelFormat);

int rectsCount = 3;                   // Generate 3 Rectangles per line...
int step = data.Stride / rectsCount;  // ...of this size (single dimension, since w=h here)
int colorIdx = 0, col = 0;            // Color and Column positions

byte[] buffer = new byte[data.Stride * image.Height];

for (int i = 1; i <= rectsCount; i++) {
    for (int y = 0; y < data.Height; y++) {
        for (int x = col; x < (step * i); x++) {
            buffer[x + y * data.Stride] = (byte)colorIdx;
        }
        colorIdx += (y + 1) % step == 0 ? 1 : 0;
    }
    col += step;
}

Marshal.Copy(buffer, 0, data.Scan0, buffer.Length);
image.UnlockBits(data);

这会生成这个有趣的图像(放大 x25):

ImageAttributes SetColorMap 原始颜色

现在,您要使调色板中的两种颜色透明:Color.PinkColor.Green
您可以构建一个对象数组ColorMap,指定哪些新颜色替换现有颜色:

var mapPink = new ColorMap() { OldColor = Color.Pink, NewColor = Color.Transparent };
var mapGreen = new ColorMap() { OldColor = Color.Green, NewColor = Color.Transparent };
var colorMap = new ColorMap[] { mapPink, mapGreen };

然后:

  • 将图像调色板中的每个颜色替换为新的映射颜色:(
    请注意,我没有[Image].Palette直接传递对象,我使用的是之前创建的调色板的副本(var palette = image.Palette;):如果您直接传递图像调色板,则更改未注册)
private ColorPalette RemapImagePalette(ColorPalette palette, ColorMap[] colorMaps)
{
    for (int i = 0; i < palette.Entries.Length; i++) {
        foreach (ColorMap map in colorMaps) {
            if (palette.Entries[i] == map.OldColor) {
                palette.Entries[i] = map.NewColor;
            }
        }
    }
    return palette;
}

// [...]

var palette = image.Palette;
image.Palette = RemapImagePalette(palette, colorMap);
  • 或者使用该方法生成一个新的位图,并使用接受参数ImageAttributes.SetRemapTable()的方法使用新的颜色图绘制索引图像:Graphics.DrawImage()ImageAttributes
private Bitmap ImageRemapColors(Image image, Size newSize, ColorMap[] map)
{
    var bitmap = new Bitmap(newSize.Width, newSize.Height);

    using (var g = Graphics.FromImage(bitmap))
    using (var attributes = new ImageAttributes()) {
        if (map != null) attributes.SetRemapTable(map);

        g.InterpolationMode = InterpolationMode.NearestNeighbor;
        g.PixelOffsetMode = PixelOffsetMode.Half;
        g.DrawImage(image, new Rectangle(Point.Empty, newSize),
                    0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
    }
    return bitmap;
}

// [...]

// Draws the 12x12 indexed 8bpp Image to a new 300x300 32bpp Bitmap
Bitmap remappedImage = ImageRemapColors(image, new Size(300, 300), colorMap);

这些方法生成相同的输出。

  • 可用于更改索引图像格式的调色板。
  • 另一种是在设备上下文中显示带有重新映射颜色的图像(例如,将位图分配给控件的图像属性)。
  • [额外] 另一种选择是使用 TextureBrush,如下所示:
    如何在图像上绘制透明形状

ImageAttributes SetColorMap 重新映射的颜色


推荐阅读