绘制 8bpp 位图时如何使调色板中的颜色透明

Kaz*_*gaa 4 c# transparency gdi+ bitmap winforms

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

using var imageAttr = new ImageAttributes();
imageAttr.SetColorKey(pink, pink, ColorAdjustType.Default);
Run Code Online (Sandbox Code Playgroud)

进而

g.DrawImage(bitmap, destRect, X, Y, Width, Height, GraphicsUnit.Pixel, imageAttr);
Run Code Online (Sandbox Code Playgroud)

它按应有的方式绘制位图,但仅对Color.Pink透明颜色进行更改。我怎样才能对第二种颜色(Color.Green)也执行此操作?

Jim*_*imi 6

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

您需要更改索引图像ColorPalette或使用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[Math.Abs(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);
Run Code Online (Sandbox Code Playgroud)

它生成了这个有趣的图像(缩放 25 倍):

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 };
Run Code Online (Sandbox Code Playgroud)

然后:

  • 将图像调色板中的每个颜色替换为新映射的颜色:(
    请注意,我没有[Image].Palette直接传递对象,我使用的是之前创建的调色板的副本 ( var palette = image.Palette;):如果直接传递图像调色板,则更改未注册)
var palette = image.Palette;
image.Palette = RemapImagePalette(palette, colorMap);

// [...]

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;
}
Run Code Online (Sandbox Code Playgroud)
  • 或者使用该方法生成一个新的位图,并使用接受参数的方法ImageAttributes.SetRemapTable()绘制带有新颜色图的索引图像:Graphics.DrawImage()ImageAttributes
// Draws the 12x12 indexed 8bpp Image to a new 300x300 32bpp Bitmap
Bitmap remappedImage = ImageRemapColors(image, new Size(300, 300), colorMap);

// [...]

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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

ImageAttributes SetColorMap 重新映射颜色