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