Search code examples
c#winformspanelsystem.drawingdouble-buffering

Double buffering with Panel


Double buffering the whole form can be done by setting the value of the "AllPaintingInWmPaint", "UserPaint" and "DoubleBuffer" ControlStyles to "true" (this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true)).

But this can't happen with a System.Windows.Forms.Panel because the class doesn't allow me to do so. I have found one solution: http://bytes.com/topic/c-sharp/answers/267635-double-buffering-panel-control . I have also tried this: Winforms Double Buffering . It's laggy, even when it's used on a small drawing, I have some custom resources that I'm using in the form and other things because of which I won't turn the whole form into one drawing. And the second one seems to cause problems. Are there other ways to do that?

I'm asking this because I don't want the drawing on the panel to flash all the time when the form is being resized. If there is a way to get rid of the flashing without double buffering, I'll be happy to know.


Solution

  • I should have posted my solution a long time ago...

    Well, here is my solution:

    Bitmap buffer = new Bitmap(screenWidth, screenHeight);//set the size of the image
    System.Drawing.Graphics gfx = Graphics.FromImage(buffer);//set the graphics to draw on the image
    drawStuffWithGraphicsObject(gfx);//draw
    pictureBox1.Image = buffer;//set the PictureBox's image to be the buffer
    

    Makes me feel like a complete idiot for finding this solution years after asking this question.

    I have tried this with a Panel, but it has proven to be slower when applying the new image. Somewhere I had read, that it is better to use Panel instead of PictureBox. I don't know if I have to add something to the code to speed things up for the Panel, though.