Too much screen flickering

T

trant

I am implementing 2D drawing in a Panel control (drawing rectangles and then
moving them) and I am getting major flickering.

I tried implementing one technique of double buffering where I create an off
screen Bitmap object and draw to it all my shapes, then in the Paint method I
draw the whole bitmap at once.

But this method is not helping.

When I googled for some help I found that someone with the same problem was
suggested to create a custom control and give it a style like this:

class MyPanel : Panel
{
public MyPanel()
{
this.SetStyle(
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.SupportsTransparentBackColor,
true
);
}
}

However as soon as I did this and hit Run my program crashes with the error:
"Parameter is not valid.". If I commented out that one part it works fine.

I am using VS 2008

Any ideas?

In the rest of my app, here are the relevant bits of code:

// setting up my drawing panel
panel = new MyPanel();
panel.Paint += new System.Windows.Forms.PaintEventHandler(this.CustomPaint);

// paint method
private void CustomPaint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
Graphics gfx = e.Graphics;
gfx.DrawImage(staticScreen, 0, 0, staticScreen.Width,
staticScreen.Height);
gfx.Dispose();
}

// where staticScreen is my background Bitmap object which I actually draw
my shapes to
 
C

Chris

I am implementing 2D drawing in a Panel control (drawing rectangles and then
moving them) and I am getting major flickering.

I tried implementing one technique of double buffering where I create an off
screen Bitmap object and draw to it all my shapes, then in the Paint method I
draw the whole bitmap at once.

But this method is not helping.

When I googled for some help I found that someone with the same problem was
suggested to create a custom control and give it a style like this:

class MyPanel : Panel
{
public MyPanel()
{
this.SetStyle(
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.SupportsTransparentBackColor,
true
);
}
}

However as soon as I did this and hit Run my program crashes with the error:
"Parameter is not valid.". If I commented out that one part it works fine.

I am using VS 2008

Any ideas?

In the rest of my app, here are the relevant bits of code:

// setting up my drawing panel
panel = new MyPanel();
panel.Paint += new System.Windows.Forms.PaintEventHandler(this.CustomPaint);

// paint method
private void CustomPaint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
Graphics gfx = e.Graphics;
gfx.DrawImage(staticScreen, 0, 0, staticScreen.Width,
staticScreen.Height);
gfx.Dispose();
}

// where staticScreen is my background Bitmap object which I actually draw
my shapes to

See if this link helps you:

http://www.bobpowell.net/doublebuffer.htm
 
B

Ben Voigt [C++ MVP]

trant said:
I am implementing 2D drawing in a Panel control (drawing rectangles
and then moving them) and I am getting major flickering.

I tried implementing one technique of double buffering where I create
an off screen Bitmap object and draw to it all my shapes, then in the
Paint method I draw the whole bitmap at once.

But this method is not helping.

When I googled for some help I found that someone with the same
problem was suggested to create a custom control and give it a style
like this:

class MyPanel : Panel
{
public MyPanel()
{
this.SetStyle(
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.SupportsTransparentBackColor,
true
);
}
}

However as soon as I did this and hit Run my program crashes with the
error: "Parameter is not valid.". If I commented out that one part it
works fine.

Try doing it in separate steps:

this.SetStyle(ControlStyles.UserPaint,true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint,true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor,true);
 
P

Peter Duniho

[...]
When I googled for some help I found that someone with the same problem
was
suggested to create a custom control and give it a style like this:

class MyPanel : Panel
{
public MyPanel()
{
this.SetStyle(
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.SupportsTransparentBackColor,
true
);
}
}

However as soon as I did this and hit Run my program crashes with the
error:
"Parameter is not valid.". If I commented out that one part it works
fine.

You should try just setting the DoubleBuffered property to "true" rather
than messing with the control styles. Normally, setting that property is
sufficient for enabling double-buffering, and is less likely to cause
other complications (for example, a common mistake is setting the
"AllPaintingInWmPaint" style, but then failing to completely redraw the
entire control during updates.

Pete
 

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