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
 

Animate Image on Frame using Drawing.ImageAnimator class vb.net – C#.net.

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

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.
C#
Bitmap image = new Bitmap("a.gif");
VB
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.
C#
private void OnFrameChanged(object o, EventArgs e)
        {
            this.Invalidate();
        }
VB
Private Sub OnFrameChanged(ByVal As ObjectByVal As EventArgs)
        
Me.Invalidate
    
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.
C#
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)
            check 
= 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.
C#
protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.DrawImage(image, new Point(0, 0));
            animateImage();
            ImageAnimator.UpdateFrames();
        }
Protected Overrides Sub OnPaint(ByVal As PaintEventArgs)
        e.Graphics.DrawImage(image, 
New Point(00))
        animateImage
        ImageAnimator.UpdateFrames
    
End Sub
This is simple code to animate the image on form.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Devasp Image Animation Application";             
        }
VB
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 vb.net – C#.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net