Double-Buffering Exception

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
 
V

Val Savvateev

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).
 
D

Dave Veeneman

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.
 
P

Piers Lawson

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
 
P

Piers Lawson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top