0 out of 1 rated this helpful Rate this topic

ImageAnimator.Animate Method

Displays a multiple-frame image as an animation.

Namespace:  System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)
public static void Animate(
	Image image,
	EventHandler onFrameChangedHandler
)

Parameters

image
Type: System.Drawing.Image
The Image object to animate.
onFrameChangedHandler
Type: System.EventHandler
An EventHandler object that specifies the method that is called when the animation frame changes.

This Windows Forms application demonstrates how to draw an animated image to the screen. The image is created from the animated GIF file SampleAnimation.gif located in the same folder as the application.


using System;
using System.Drawing;
using System.Windows.Forms;

public class animateImage : Form 
{

    //Create a Bitmpap Object.
    Bitmap animatedImage = new Bitmap("SampleAnimation.gif");
    bool currentlyAnimating = false;

    //This method begins the animation.
    public void AnimateImage() 
    {
        if (!currentlyAnimating) 
        {

            //Begin the animation only once.
            ImageAnimator.Animate(animatedImage, new EventHandler(this.OnFrameChanged));
            currentlyAnimating = true;
        }
    }

    private void OnFrameChanged(object o, EventArgs e) 
    {

        //Force a call to the Paint event handler.
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e) 
    {

        //Begin the animation.
        AnimateImage();

        //Get the next frame ready for rendering.
        ImageAnimator.UpdateFrames();

        //Draw the next frame in the animation.
        e.Graphics.DrawImage(this.animatedImage, new Point(0, 0));
    }

    public static void Main() 
    {
        Application.Run(new animateImage());
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
The OnAnimate Callback is invoked on background thread.
It's important to note that the callback supplied to the Animate method is called on the animators background thread, therefore it's important to marshal back to the UI's thread (this.InvokeRequired for Winforms, this.Dispatcher.Invoke for WPF) before attempting to invalidate the region containing the image.