Tuesday, July 12, 2011

Preventing Flicker with GDI+ drawing in C#

In C#, the OnPaintBackground method is called before the OnPaint event and it draws the background color for the entire view. If you are already drawing the background in the OnPaint event, then this process will cause flicker. You can override the OnPaintBackground and just do nothing in it to prevent the flicker:
protected override void OnPaintBackground(PaintEventArgs pevent)
{
// do nothing
}

2 comments:

VakarimaZ said...

The easier way is to enable Double Buffer for your form/control.
Example:
class BufferedPanel : Panel
{
public BufferedPanel()
{
this.DoubleBuffered = true;
}
}

srego said...

True in some cases, but in my case I am bliting the entire view on every OnPaint, so double buffer would be a waste of both resources and performance.