Draw Bitmap with alpha channel

Posted by Paja on Stack Overflow See other posts from Stack Overflow or by Paja
Published on 2010-04-10T19:17:03Z Indexed on 2010/04/10 19:23 UTC
Read the original article Hit count: 711

Filed under:
|
|
|
|

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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about Windows