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.
You need to set DoubleBuffered
to true.
Since it's a protected property, you'll need to make your own control:
class Canvas : Control {
public Canvas() { DoubleBufferred = true; }
}
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.
People say use DoubleBufferred = true;
, but you can easily change the DoubleBufferred
parameter on the form to true
, without needing to use code.
来源:https://stackoverflow.com/questions/8298148/how-to-avoid-blinking-in-form-invalidate