Double-Buffering Exception

  • Thread starter Thread starter Dave Veeneman
  • Start date Start date
D

Dave Veeneman

I'm drawing a graphic directly to a form, using a Paint event handler. All
works well, except that the graphic flickers when I resize the form. So, I
implement double-buffering for the form using the following function:

public void EnableDoubleBuffering()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.UpdateStyles();
}

I call the function from the form's constructor, and I get an
ArgumentException saying that an invalid parameter was used.. The call stack
indicates the exception is thrown in the System.Drawing library.

The code runs fine if I disable the EnableDoubleBuffering() call, but of
course I still have flicker.

Am I missing something here? Any help figuring this out is much appreciated.

Dave Veeneman
Chicago
 
At what line are you getting the exception? I'm using double buffering in
one of my controls too. The order is a little bit different -
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

this.SetStyle(ControlStyles.UserPaint, true);

this.SetStyle(ControlStyles.DoubleBuffer, true);



I think your problem is that you call UpdateStyles method (I don't) - it has
a bit different purpose (it updates other kind of styles).
 
Thanks! I'll give that a try.

For the benefit of anyone else reading this posting-- the code I originally
used came from the DotNet SDK documentation, which means the example in that
documentation may not work correctly.
 
Did you ever find a solution? I get the same exception,
though I'm using slightly different styles:

DoubleBuffer, AllPaintingInWmPaint, UserPaint, Opaque
ResizeRedraw.

I get the exception:

System.ArgumentException: Invalid parameter used.

By dropping the AllPaintingInWmPaint the problem went
away, but of course my flicker returns.

Piers
 
I just looked through the call exception stack and noticed
Dispose was in there. At the end of my OnPaint method I
have been calling e.Graphics.Dispose(). I removed this
line an evrything started working!

I guess with double buffering on you shouldn't Dispose of
the graphics surface?

Piers
 
Back
Top