Panel Double Buffering

You can double-buffer the entire form by setting the value "AllPaintingInWmPaint", "UserPaint" and "DoubleBuffer" ControlStyles to "true" ( this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true) ),

But this cannot happen with System.Windows.Forms.Panel, because the class does not allow me to do this. I found one solution: http://bytes.com/topic/c-sharp/answers/267635-double-buffering-panel-control . I also tried this: Winforms Double Buffering . It is laggy, even when it is used in a small drawing, I have some user resources that I use in the form and other things, because of which I will not turn the entire form into one drawing. And the second seems to be causing problems. Are there any other ways to do this?

I ask this because I do not want the panel image to blink all the time when the shape changes. If there is a way to get rid of blinking without double buffering, I will be glad to know.

+4
source share
3 answers

I had to publish my decision 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 image to be the buffer 

It makes me feel like a complete idiot to find this solution years after I asked this question.

I tried this with Panel, but when applying a new image, it turned out to be slower. Somewhere I read that it is better to use Panel instead of PictureBox. I don’t know if I need to add something to the code to speed up the panel.

+1
source

Use PictureBox if you do not need scrolling support, by default it is a double buffer. Getting a scrollable panel with double buffering is quite simple:

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

Purpose ResizeRedraw suppresses drawing optimizations for container controls. You will need this if you do any painting in the panel. Without it, the picture is smeared when the panel is resized.

Double buffering actually makes the picture slower. This may affect controls that are drawn later. The hole that they leave before filling may be visible for a while, also perceived as flickering. You will find counter measures against the effect in this answer .

+9
source

If you can, you can stop updating the panel when resizing and turn it back on after that, so you get rid of the ugly flicker.

+1
source

Source: https://habr.com/ru/post/1393729/


All Articles