0

我正在尝试在位图上“绘制”透明区域 - 就像在图像中切割孔一样。

以下代码不会绘制透明线,因为在位图上绘制透明线当然是混合而不是替换。(为什么默认是做两个绘图操作中更复杂的,没有意义。)

Bitmap myBitmap = new Bitmap(50, 50);
Graphics g = Graphics.FromImage(myBitmap);
g.FillRectangle(Brushes.Black, 0, 0, 50, 50);
g.FillEllipse(Brushes.Transparent, 25, 0, 25, 25); //Does nothing
g.DrawLine(Pens.Transparent, 0, 0, 50, 50); //Does nothing

我将如何修改它以使透明的圆圈和线条替换位图中的内容而不是混合?

(请注意,这是“绘制”完全透明的小例子。我要达到的目的是能够“绘制”修改 Alpha 通道,而无需创建我自己的逐个像素操作。能够做到完全透明将不过就够了。)


在建议重复的文章中的答案之后,我还尝试了以下方法(不起作用)

        base.OnPaint(e);
        Bitmap myBitmap = new Bitmap(50, 50);
        e.Graphics.FillRectangle(Brushes.Black, 0, 0, 50, 50);
        Graphics g = Graphics.FromImage(myBitmap);
        g.FillEllipse(new SolidBrush(Color.FromArgb(150, 125, 125, 125)), 25, 0, 25, 25);
        g.DrawLine(new Pen(Color.FromArgb(150,25,25,25)), 0, 0, 50, 50);
        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
        e.Graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceOver;
        e.Graphics.DrawImage(myBitmap, 0, 0);

还测试了这个SourceCopy

4

1 回答 1

2

这对我来说完全没问题

Bitmap bmp = new Bitmap(50, 50, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
    g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
    g.FillRectangle(Brushes.Black, 0, 0, 50, 50);
    g.FillEllipse(Brushes.Transparent, 25, 0, 25, 25);
    g.DrawLine(Pens.Transparent, 0, 0, 50, 50);
    g.Flush();
}
bmp.Save("Test.bmp");
于 2013-02-22T21:53:06.997 回答