绘制透明图像不起作用

Drawing transparent image doesn't work

我有一个背景颜色设置为透明的 PictureBox 控件。我想在此控件上绘制图像(来源:https://en.wikipedia.org/wiki/Seven-segment_display#/media/File:7-segment.svg),但它的背景颜色不是透明背景,而是白色(不幸的是,绘图不支持 alpha 通道)。

以下是我尝试绘制位图的方法:

private void DrawPictureBox() 
{
    pbScreen.Image = Update();
}

private Bitmap CreateBackgroundBitmap()
{
    Bitmap bitmap = (Bitmap)Properties.Resources.ResourceManager.GetObject("empty");
    bitmap.MakeTransparent(Color.White);

    return bitmap;
}

private ImageAttributes GetImageAttributes()
{
    float[][] matrixItems = {
        new float[] {1, 0, 0, 0, 0},
        new float[] {0, 1, 0, 0, 0},
        new float[] {0, 0, 1, 0, 0},
        new float[] {0, 0, 0, Contrast, 0},
        new float[] {0, 0, 0, 0, 1}};

    ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

    ImageAttributes imageAtt = new ImageAttributes();
    imageAtt.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

    return imageAtt;
}

private void DrawSegment(ref Graphics g, Digit digit, int Position = 0)
{
    if (digit == null)
        return;

    Bitmap buffer = digit.CreateBitmapFromFile(digit.CreateFileNameFromContent());
    buffer.MakeTransparent(Color.White);

    g.DrawImage(buffer, new Rectangle(0 + Position * 43, 0, 43, 67), 0.0f, 0.0f, 43, 67, GraphicsUnit.Pixel, this.GetImageAttributes());

}

public Bitmap Update()
{
    Bitmap buffer = CreateBackgroundBitmap();

    Graphics g = Graphics.FromImage(buffer);

    g.DrawImage(buffer, new Rectangle(0, 0, 301, 67), 0.0f, 0.0f, 301, 67, GraphicsUnit.Pixel, this.GetImageAttributes());

    if (State == Powerstate.on)
    {
        // Draw segments
        for (int i = 0; i < digits.Count(); i++)
        {
            DrawSegment(ref g, digits[i], i);

            if (digits[i]?.Dot == Dot.dot_on)
            {
                DrawPoint(ref g, digits[i], i);
            }
        }
    }

    return buffer;
}

绘制线段(绘制点类似)按预期工作,但绘制背景不会接受由 'GetImageAttributes()' 创建的透明度。

另一个奇怪的事情是,如果我添加

g.Clear(color: Color.Black);

之前

g.DrawImage(buffer, new Rectangle(0, 0, 301, 67), 0.0f, 0.0f, 301, 67, GraphicsUnit.Pixel, this.GetImageAttributes());

在 Update() 方法中,我的图形 'g' 只是黑色 - 无论我是保留还是删除

bitmap.MakeTransparent(Color.White);

在 'CreateBackgroundBitmap()' 中,但在黑色背景上绘制线段是否按预期工作?!

有人知道问题出在哪里吗?我想念什么?

非常感谢:)

好的,我明白了。我想我不明白 Graphics 对象如何与位图交互,图形对象是由什么构成的。

这是必须更改的方式:

public Bitmap Update()
{
    Bitmap background = new Bitmap(301, 67);
    Graphics g = Graphics.FromImage(background);

    g.Clear(color: Color.White);

    Bitmap buffer = CreateBackgroundBitmap();

    g.DrawImage(buffer, new Rectangle(0, 0, 301, 67), 0.0f, 0.0f, 301, 67, GraphicsUnit.Pixel, this.GetImageAttributes());

    if (State == Powerstate.on)
    {
        for (int i = 0; i < digits.Count(); i++)
        {
            DrawSegment(ref g, digits[i], i);

            if (digits[i]?.Dot == Dot.dot_on)
            {
                DrawPoint(ref g, digits[i], i);
            }
        }
    }

    return background;
}