4

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
AlexSavAlexandrov
  • 858
  • 2
  • 13
  • 34
  • I think it depends on what you are trying to do. Are you trying to perform refreshes of data or animation? Are you trying to make a game? I remember it being very difficult to remove flicker using system.drawing, even with double buffering (sometimes the frame rate is so slow that it looks like flicker even when double buffered) – keithwill Sep 27 '11 at 12:59
  • It's a game that is played in a WinForms window. The drawing is drawn on a panel. The drawing is being changed when the user clicks on the panel. But when there are the scrolling and resizing processes, there is flickering too. – AlexSavAlexandrov Sep 27 '11 at 13:07
  • You can find a solution here, it worked for me. [Solution][1] [1]: http://stackoverflow.com/questions/16882921/c-sharp-panel-flickering-when-redrawing?answertab=votes#tab-top – Ahmed Yossef Apr 16 '14 at 09:59

3 Answers3

11

Use a PictureBox if you don't need scrolling support, it is double-buffered by default. Getting a double-buffered scrollable panel is easy enough:

using System;
using System.Windows.Forms;

class MyPanel : Panel {
    public MyPanel() {
        this.DoubleBuffered = true;
        this.ResizeRedraw = true;
    }
}

The ResizeRedraw assignment suppresses a painting optimization for container controls. You'll need this if you do any painting in the panel. Without it, the painting smears when you resize the panel.

Double-buffering actually makes painting slower. Which can have an effect on controls that are drawn later. The hole they leave before being filled may be visible for a while, also perceived as flicker. You'll find counter-measures against the effect in this answer.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I can try to get rid of some controls, but they won't be many... I'll try making a new Panel class and "unlock" some of the System.Windows.Forms.Panel's features. But thanks for the link. This is a handy tutorial. – AlexSavAlexandrov Sep 27 '11 at 13:48
  • Hans, how do you exchange the old panel with this new one? I wrote this class, now how do I connect it to my existing `panel1` so it gets the aforementioned attributes? – Romy Jun 22 '16 at 20:38
  • Remove panel1 from your form and drop this one in its place. You can rename it to "panel1" to keep existing code working. – Hans Passant Jun 22 '16 at 20:39
  • I've tried that and I'm getting ambiguity errors between the "panel1'"s – Romy Jun 22 '16 at 20:42
3

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.

AlexSavAlexandrov
  • 858
  • 2
  • 13
  • 34
1

If acceptable you can stop refreshing the panel while resizing and enable it again after, this way you get rid of the ugly flickering.

StefanE
  • 7,578
  • 10
  • 48
  • 75