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 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; }
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With