| Double Buffering a Panel |
These are all changes in the PanelSpecial class It requires two more class variables:
Bitmap offScreenBmp; Graphics offScreenDC;which are initialized in the constructor:
offScreenBmp = new Bitmap(this.Width, this.Height); offScreenDC = Graphics.FromImage(offScreenBmp);Note that this.Width and this.Height aren't necessarily set at the time of construction, so you have to handle the resize event and reinitialize this backbuffer:
private void PanelSpecial_Resize(object sender, EventArgs e)
{
offScreenBmp = new Bitmap(this.Width, this.Height);
offScreenDC = Graphics.FromImage(offScreenBmp);
}
Now every time paint is called, the drawing is done to this invisible
Graphics object, then the associated Bitmap is drawn to the panel
Graphics:
offScreenDC.FillRectangle(Brushes.AliceBlue, 0, 0, this.Width, this.Height); DrawAll(offScreenDC); clientDC.DrawImage(offScreenBmp, 0, 0);In this case, all my drawing is handled by the DrawAll method. I'm sure there is a way to get SetStyle to do the double buffering automatically, but there are a lot of variables that can make it go very wrong, or as is my experience with it. - Dennis Lipovsky, Nov. 2005
Visitors:![]()