C#, Overriding OnPaint: alpha transparency with double buffer

ccompact-frameworkdoublebufferedonpaintwindows-mobile

I'm developing a Windows Mobile 5.0 and above application with .Net Compact Framework 2.0 SP2 with C#.

I'm overriding OnPaint method on a custom messagebox that draw a bitmap that fills the entire form with alpha transparency, and a gradient box with a button and a message over the semi-transparent background.

I'm testing it but it is so slow, so I'm going to use double buffer. I can use double buffer to draw the gradient box and the test, but if I use double buffer with the background bitmap with alpha transparency it doen't paint the alpha transparency. So I only do double buffer with gradient box and message and button. The bacground transparent bitmap is painted directly on e.Graphics.

I wondering if I can save e.Graphics on a bitmap to make all the work and end the OnPaint method drawing to e.Graphics this bitmap that I save it before.

This is my code:

protected override void OnPaint(PaintEventArgs e)
{
    Graphics gxOff;
    gxOff = Graphics.FromImage(bmpOffscreen);

    if (!isOuterBackgroundPainted)
    {
        isOuterBackgroundPainted = true;
        DrawingHelper.DrawAlpha(e.Graphics, outerBackground, 180, 0, 0);
        // Here I don't use double buffer because Alpha Blend doesn't work with double buffer.
        //DrawingHelper.DrawAlpha(gxOff, outerBackground, 180, 0, 0);
    }

    // Draw the gradient box
    GradientFill.Fill(gxOff, rectangle, startColor, endColor, FillDirection.TopToBottom);

    gxOff.DrawString(message, font, brush, textLayoutRectangle);

    e.Graphics.DrawImage(bmpOffscreen, 10, 10);
    base.OnPaint(e);
}

bmpOffscreen: double buffer's bitmap.

Maybe I can get a snapshop of form into bmpOffscreen and then draw semi-transparent background over it, gradient box and text.

Summarizing: I want to use alpha blend with double buffer.

Any advice?

Best Answer

Exactly how to do this is actually pretty complicated - more complicated than a simple answer here can provide. Take a look at the source for Project Resistance. We have a double-buffered Form and we're painting in a background and controls with a transparency alpha channel.

Related Topic