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

Animate Image on Frame using Drawing.ImageAnimator class –

Author: Shehzad Hemani
Download Source Code :

In this simple article you will learn that how we can animate multi frame image on form using Drawing.ImageAnimator class in VB and C# .net.


Animate Image:
Bitmap Object:
To animate image on form first of all we have to declare an object of bitmap class. This will store the image.
Bitmap image = new Bitmap("a.gif");
Dim image As Bitmap = New Bitmap("a.gif")
OnFrameChanged Event:
Now after this we will have to create an event on which we will invalidate the form region and again redraw the frame region. This event will use by imageAnimator class method. Here I am creating it with name OnFrameChanged.
private void OnFrameChanged(object o, EventArgs e)
Private Sub OnFrameChanged(ByVal As ObjectByVal As EventArgs)
End Sub
AnimateImage Method:
Now make a method in which we will call imageanimator class method to animate image. This method has name animateimage ().l it takes and returns nothing.
void animateImage()
            if (check == false)
                ImageAnimator.Animate(image, new EventHandler(this.OnFrameChanged));
                check = true;
Private Sub animateImage()
If (check = falseThen
ImageAnimator.Animate(image, AddressOf Me.OnFrameChanged)
= true
        End If
    End Sub
In this you can see that we are using method of imageAnimate class. This method has named animate. This is a static function. It takes the bitmap class object and event handler. This will call that event again and again as the frame changed and on that event the control will redraw to show the new frame. This function starts animation only once. To ensure that this function should be called only once we use check variable. Initialize check with false then make it true in function so that animate function will not call again and again.
On Paint Method:
We will have to override onPaint method. In this method we will have to call animateimage function and have to draw the image on form. We have to update frame each time when on paint method is called.
protected override void OnPaint(PaintEventArgs e)
            e.Graphics.DrawImage(image, new Point(0, 0));
Protected Overrides Sub OnPaint(ByVal As PaintEventArgs)
New Point(00))
End Sub
This is simple code to animate the image on form.
Now write the following code on FORM LOAD event:
private void Form1_Load(object sender, EventArgs e)
            this.Text = "Devasp Image Animation Application";             
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Me.Text = "Devasp Image Animation Application" 
    End Sub

This simple article tells that how we can animate multi frame image on form using Drawing.ImageAnimator class in VB and C# .net.

Add Article Comment:
Name :
Email Address :
Comments :
<< Animate Circle on Frame using Graphics and Thread class –

Disclaimer - Privacy
© 2002-2018