Tutors Corner Moderated forum that should hold answers to the FAQs we have here. If you are new check this forum first for the answer to your question.

Go Back  Xtreme .NET Talk > Knowledge Base > Tutors Corner > Tutorial: Double Buffering using GDI BitBlt


Reply
 
Thread Tools Display Modes
  #1  
Old 06-12-2005, 08:18 PM
qmp qmp is offline
Newcomer
 
Join Date: May 2005
Posts: 14
qmp is on a distinguished road
Default Tutorial: Double Buffering using GDI BitBlt

With help from this forum I was able to figure out the double buffer solution using gdi32's BitBlt in C#.net.

GDI+ is well known for its lack of speed in the performance arena.

To solve the "flicker" solution, and add a fast back buffer to my application, i dipped back into gdi32.

What you'll want to do is this..

On your form, create a picturebox that will show your live drawing. Lets call it picChart.

Now, what we need to do is create a back buffer for our picChart.
Code:
private Bitmap memBmp;  // Backbuffers bitmap
private IntPtr hMemBmp; // Handle to our memBmp
private Graphics memDC; // We draw on this
private IntPtr hMemdc;  // Handle to our memDC
private void picChart_Resize(object sender, EventArgs e)
{
    // If we have already created a dc for mem, lets free it up first
    if (hMemdc.ToInt32() != 0)
    {
        // Clean up
        DeleteObject(hMemBmp);
        DeleteDC(hMemdc);
        memBmp.Dispose();
    }

    // Resize our backbuffer
    memBmp = new Bitmap(picChart.Width, picChart.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555 );
    Graphics clientDC = picChart.CreateGraphics();

    IntPtr hdc = clientDC.GetHdc();
    hMemdc = CreateCompatibleDC(hdc);   // hMemdc is the handle to our memDC
    hMemBmp = memBmp.GetHbitmap();
    SelectObject(hMemdc, hMemBmp);
    memDC = Graphics.FromHdc(hMemdc);   // We draw on memDC

    // Clean up
    clientDC.ReleaseHdc(hdc);
    clientDC.Dispose();
}

private void picChart_Paint(object sender, PaintEventArgs e)
{
    // Copy Region from memDC
    IntPtr hdc = e.Graphics.GetHdc();

    BitBlt(hdc, e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height, hMemdc, e.ClipRectangle.X, e.ClipRectangle.Y, 0x00CC0020);

    e.Graphics.ReleaseHdc(hdc);
}

What the above code does is create a backbuffer the same size of our picChart every time the picChart is resized. If we have already created a backbuffer from a previous resize event, we delete the previous backbuffer from memory.

Then in the picChart paint event, we will bitblt the exact region that was invalidated to our main picChart paint area.

Also, in your form/controls dispose() method you will need to add:
Code:
// If we have already created a dc for mem, lets free it up first
    if (hMemdc.ToInt32() != 0)
    {
        // Clean up
        DeleteObject(hMemBmp);
        DeleteDC(hMemdc);
        memBmp.Dispose();
    }

That will free up your backbuffer if it was created before the form/control disposes.


To draw on your backbuffer, all you have to use is memDC.Drawxxx to draw whatever you like.

It still uses GDI+ drawing techniques but will use the faster gdi32 bitblt to doublebuffer and repaint your form.

Hope this helps.

Last edited by PlausiblyDamp; 06-14-2005 at 05:01 AM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Serious memory leak, GDI, manual double buffer, bitblt qmp Graphics and Multimedia 1 06-08-2005 05:32 PM
buffering tutorial jspeirer Directory / File IO / Registry 0 10-03-2003 10:17 AM
Direct Draw - Double Buffering? snarfblam DirectX 1 08-08-2003 07:55 PM
Double Buffering in Memory JCreationsInc Graphics and Multimedia 2 05-03-2003 04:26 PM

Advertisement:

Powered by liquidweb