|
|
Title | Use double buffering to prevent flicker when drawing graphics in Visual Basic .NET |
Description | This example shows how to use double buffering to prevent flicker when drawing graphics in Visual Basic .NET. |
Keywords | graphics, buffer, double buffer, double buffering, double-buffering, curve, butterfly, butterfly curve, algorithms, fractals, colors, math, mathematics, Visual Basic .NET, VB.NET |
Categories | Graphics, VB.NET |
|
|
If you draw a very complex drawing in a form's Paint event handler, the user may see the drawing flicker as different parts are drawn. You can eliminate that flickering by using double buffering. To use double buffering, simply set a form's DoubleBuffer property to true.
When you use double buffering, the system allocates a seperate piece of memory to holds the form's image. When the Paint event handler draws on the form, the system draws on this in-memory image. When it is finished, the system displays the form's image all at once. If the drawing is complicated, as in this example, that prevents the user from seeing the flickering image as the program draws.
When you check this example's checkbox, the program enables or disables double buffering and redraws. You can also click the Redraw button to force a redraw.
|
|
' Turn double buffering on or off.
Private Sub chkDoubleBuffer_CheckedChanged(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
chkDoubleBuffer.CheckedChanged
Me.DoubleBuffered = chkDoubleBuffer.Checked
' Redraw.
Me.Invalidate()
End Sub
' Redraw.
Private Sub btnRedraw_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnRedraw.Click
Me.Invalidate()
End Sub
|
|
When double buffering is off, you should see the flicker. When double buffering is on, the redraw happens so smoothly that you probably won't notice anything. Resize the form to force a redraw that is more noticeable.
Double buffering does take some extra memory so you should only use it when you will do enough drawing to make the flicker visible.
|
|
|
|
|
|