views:

220

answers:

1

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.

+2  A: 

Set Graphics.CompositingMode to CompositingMode.SourceCopy before drawing your image.

Matti Virkkunen
Thank you very much, that was very quick! :-) It works just great of course...
Paja