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

Animation with double buffering – avoid - remove flickers using Graphics and Thread class vb.net – C#.net.

Author: Shehzad Hemani
Download Source Code : 1079_source.zip

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

 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:

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:

C#

private bool reduce = false;

private int size = 0;

private void timer1_Tick(object sender, EventArgs e)

        {

            if (reduce)

                size--;

            else

                size++;

            if (size > 500)

                reduce = true;

            else if (size < 1)

                reduce = false;

            this.Invalidate();

        }

VB

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

Now write the following code on Form Paint Event.

C#

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

        }

VB

Private Sub Paint_Event(ByVal sender As ObjectByVal As PaintEventArgs)
        
Me.DoubleBuffered chkDoubleBuffer.Checked
        
Dim As Graphics e.Graphics
        g.SmoothingMode 
System.Drawing.Drawing2D.SmoothingMode.HighQuality
        
Dim drawingPen As Pen = New Pen(Color.Black, 10)
        g.FillRectangle(Brushes.White, 
New Rectangle(New Point(00), Me.ClientSize))
        g.DrawRectangle(drawingPen, 
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:

C#

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "Devasp Animation Application";

            timer1.Interval = 50;

            timer1.Start();

        }

VB

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

This simple article tells that how we can do animation with double buffering using Graphics and Thread class vb.net – C#.net

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 vb.net – C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net