Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Animation with double buffering – avoid - remove flickers using Graphics and Thread class –

Author: Shehzad Hemani
Download Source Code :

In this simple article you will learn that how we can do animation with double buffering using Graphics and Thread class –

 Double Buffer:

Double buffer means to use secondary buffer to redraw control to reduce or prevent the flickers. Double buffer is a property of form class. This property takes a Boolean value. True means start the double buffering and false means stop the double buffering.


Invalidate is a function of form class which forces the control to redraw. This function basically triggers the paint event of the control. We will use this function under tick event of timer control. This function will call after some specific time that will force control to redraw or it will call paint event of the form.

First draw a rectangle on the form under the paint event of the form and then increase or decrease the size of the rectangle in timer control tick event.

Now write the following code on Timer Tick Event:


private bool reduce = false;

private int size = 0;

private void timer1_Tick(object sender, EventArgs e)


            if (reduce)




            if (size > 500)

                reduce = true;

            else if (size < 1)

                reduce = false;




Private Sub timer1_Tick(ByVal sender As ObjectByVal As EventArgs)
If reduce Then
size (size - 1)
size (size + 1)
End If
(size > 500Then
reduce = true
(size < 1Then
reduce = false
        End If
End Sub

Now write the following code on Form Paint Event.


private void Paint_Event(object sender, PaintEventArgs e)


            this.DoubleBuffered = chkDoubleBuffer.Checked;

            Graphics g = e.Graphics;

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            Pen drawingPen = new Pen(Color.Black, 10);

            g.FillRectangle(Brushes.White, new Rectangle(new Point(0, 0), this.ClientSize));

            g.DrawRectangle(drawingPen, 50, 50, 50 + size, 50 + size);



Private Sub Paint_Event(ByVal sender As ObjectByVal As PaintEventArgs)
Me.DoubleBuffered chkDoubleBuffer.Checked
Dim As Graphics e.Graphics
Dim drawingPen As Pen = New Pen(Color.Black, 10)
New Rectangle(New Point(00), Me.ClientSize))
5050, (50 + size), (50 + size))
End Sub

All above shown code is to do animation with double buffering.

Now write the following code on FORM LOAD event:


private void Form1_Load(object sender, EventArgs e)


            this.Text = "Devasp Animation Application";

            timer1.Interval = 50;




Private Sub Form1_Load(ByVal sender As ObjectByVal As EventArgs)
Me.Text "Devasp Animation Application"
timer1.Interval 50
End Sub

This simple article tells that how we can do animation with double buffering using Graphics and Thread class –

Article Comments
Thank you Shehzad - what a fantastic, simple and effective example! I've searched a long time for this solution, and it's made a massive difference to my application. Well done.

Posted on 11/2/2010 5:39:09 AM by Greg

Genius! Thank you times a million.

Posted on 11/13/2010 7:10:19 PM by Dave

Add Article Comment:
Name :
Email Address :
Comments :
<< How to draw an arrow using Graphics class –

Disclaimer - Privacy
© 2002-2017