简体   繁体   中英

Draw Bitmap with alpha channel

I have a Format32bppArgb backbuffer, where I draw some lines:

var g = Graphics.FromImage(bitmap);
g.Clear(Color.FromArgb(0));
var rnd = new Random();
for (int i = 0; i < 5000; i++) {
    int x1 = rnd.Next(ClientRectangle.Left, ClientRectangle.Right);
    int y1 = rnd.Next(ClientRectangle.Top, ClientRectangle.Bottom);
    int x2 = rnd.Next(ClientRectangle.Left, ClientRectangle.Right);
    int y2 = rnd.Next(ClientRectangle.Top, ClientRectangle.Bottom);
    Color color = Color.FromArgb(rnd.Next(0, 255), rnd.Next(0, 255), rnd.Next(0, 255));

    g.DrawLine(new Pen(color), x1, y1, x2, y2);
}

Now I want to copy bitmap in Paint event. I do it like this:

void Form1Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawImageUnscaled(bitmap, 0, 0);
}

Hovewer, the DrawImageUnscaled copies pixels and applies the alpha channel, thus pixels with alpha == 0 won't have any effect. But I need raw byte copy, so pixels with alpha == 0 are also copied. So the result of these operations should be that e.Graphics contains exact byte-copy of the bitmap . How to do that?

Summary: When drawing a bitmap, I don't want to apply the alpha channel, I merely want to copy the pixels.

在绘制图像之前,将Graphics.CompositingMode设置为CompositingMode.SourceCopy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM