Permalink
Fetching contributors…
Cannot retrieve contributors at this time
76 lines (67 sloc) 3.68 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
How to: Copy Pixels for Reducing Flicker in Windows Forms
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
bitblt
graphics, copying
flicker, reducing in Windows Forms
graphics, reducing flicker
pixels, copying
flicker
bit-block transfer
33b76910-13a3-4521-be98-5c097341ae3b
13
dotnet-bot
dotnetcontent
wpickett

How to: Copy Pixels for Reducing Flicker in Windows Forms

When you animate a simple graphic, users can sometimes encounter flicker or other undesirable visual effects. One way to limit this problem is to use a "bitblt" process on the graphic. Bitblt is the "bit-block transfer" of the color data from an origin rectangle of pixels to a destination rectangle of pixels.

With Windows Forms, bitblt is accomplished using the xref:System.Drawing.Graphics.CopyFromScreen%2A method of the xref:System.Drawing.Graphics class. In the parameters of the method, you specify the source and destination (as points), the size of the area to be copied, and the graphics object used to draw the new shape.

In the example below, a shape is drawn on the form in its xref:System.Windows.Forms.Control.Paint event handler. Then, the xref:System.Drawing.Graphics.CopyFromScreen%2A method is used to duplicate the shape.

[!NOTE] Setting the form's xref:System.Windows.Forms.Control.DoubleBuffered%2A property to true will make graphics-based code in the xref:System.Windows.Forms.Control.Paint event be double-buffered. While this will not have any discernable performance gains when using the code below, it is something to keep in mind when working with more complex graphics-manipulation code.

Example

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _  
    System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint  
    ' Draw a circle with a bar on top.  
        e.Graphics.FillEllipse(Brushes.DarkBlue, New Rectangle _  
             (10, 10, 60, 60))  
        e.Graphics.FillRectangle(Brushes.Khaki, New Rectangle _  
             (20, 30, 60, 10))  
    ' Copy the graphic to a new location.  
        e.Graphics.CopyFromScreen(New Point(10, 10), New Point _  
             (100, 100), New Size(70, 70))  
End Sub  
private void Form1_Paint(System.Object sender,  
    System.Windows.Forms.PaintEventArgs e)  
        {  
        e.Graphics.FillEllipse(Brushes.DarkBlue, new  
            Rectangle(10,10,60,60));  
        e.Graphics.FillRectangle(Brushes.Khaki, new  
            Rectangle(20,30,60,10));  
        e.Graphics.CopyFromScreen(new Point(10, 10), new Point(100, 100),   
            new Size(70, 70));  
}  

Compiling the Code

The code above is run in the form's xref:System.Windows.Forms.Control.Paint event handler so that the graphics persist when the form is redrawn. As such, do not call graphics-related methods in the xref:System.Windows.Forms.Form.Load event handler, because the drawn content will not be redrawn if the form is resized or obscured by another form.

See Also

xref:System.Drawing.CopyPixelOperation
xref:System.Drawing.Graphics.FillRectangle%2A?displayProperty=nameWithType
xref:System.Windows.Forms.Control.OnPaint%2A?displayProperty=nameWithType
Graphics and Drawing in Windows Forms
Using a Pen to Draw Lines and Shapes