Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid blinking in Form.Invalidate()?

I'm using f.Invalidate() to repaint graphics in my C# program but the graphic blinks as it refreshes. I'm also using e.Graphics.DrawImage() inside the f_Paint() method.

like image 701
Reinan Contawi Avatar asked Sep 17 '25 15:09

Reinan Contawi


2 Answers

You need to set the property DoubleBuffered to true.

Since it's a protected property, you'll need to make your own control:

class Canvas : Control {
    public Canvas() { DoubleBuffered = true; }
}
like image 132
SLaks Avatar answered Sep 20 '25 07:09

SLaks


You may need to do all of your drawing to an in memory bitmap first, then paint that bitmap to the form so that it is all drawn on screen at once.

Image buffer = new Bitmap(width, height, colorDepth); //I usually use 32BppARGB as my color depth
Graphics gr = Graphics.fromImage(buffer);

//Do all your drawing with "gr"

gr.Dispose();
e.graphics.drawImage(buffer,0,0);
buffer.Dispose();

You can be more efficient by keeping buffer around longer and not recreating it every frame. but DON'T keep gr around, it should be created and disposed each time you paint.

like image 22
Bradley Uffner Avatar answered Sep 20 '25 06:09

Bradley Uffner