SoFunction
Updated on 2025-03-07

Detailed explanation of how to draw animation examples in C# Winform

Preface

Here we introduce an ImageAnimator class carried by .net itself. This class is similar to controlling the timeline of animation. It can determine whether an image is an animation. Calling can start playing the animation, that is, every time a frame passes, OnFrameChanged delegation is triggered. We just need to select the active frame of the Image to the next frame in the delegation and force the interface to redraw the interface to achieve the animation effect.

In order to facilitate future use, I integrated these codes together to form an AnimateImage class, which provides properties such as CanAnimate, FrameCount, CurrentFrame, and common methods for animations such as Play(), Stop(), Reset(). The code is as follows

using System;  
using ;  
using ;  
using ;  
using ;  
 
namespace GifTest  
{  
  /// <summary>  
  /// Represents a type of image with animation function.  /// </summary>  
  public class AnimateImage  
  {  
    Image image;  
    FrameDimension frameDimension;  
    /// <summary>  
    /// Triggered when the current frame of the animation changes.    /// </summary>  
    public event EventHandler<EventArgs> OnFrameChanged;  
 
    /// <summary>  
    /// Instantiate an AnimateImage.    /// </summary>  
    /// <param name="img">Animation picture.  </param>    public AnimateImage(Image img)  
    {  
      image = img;  
 
      lock (image)  
      {  
        mCanAnimate = (image);  
        if (mCanAnimate)  
        {  
          Guid[] guid = ;  
          frameDimension = new FrameDimension(guid[0]);  
          mFrameCount = (frameDimension);  
        }  
      }  
    }  
 
    bool mCanAnimate;  
    int mFrameCount = 1, mCurrentFrame = 0;  
 
    /// &lt;summary&gt;  
    /// picture.    /// &lt;/summary&gt;  
    public Image Image  
    {  
      get { return image; }  
    }  
 
    /// &lt;summary&gt;  
    /// Whether to animation.    /// &lt;/summary&gt;  
    public bool CanAnimate  
    {  
      get { return mCanAnimate; }  
    }  
 
    /// &lt;summary&gt;  
    /// Total frame count.    /// &lt;/summary&gt;  
    public int FrameCount  
    {  
      get { return mFrameCount; }  
    }  
 
    /// &lt;summary&gt;  
    /// The current frame played.    /// &lt;/summary&gt;  
    public int CurrentFrame  
    {  
      get { return mCurrentFrame; }  
    }  
 
    /// &lt;summary&gt;  
    /// Play this animation.    /// &lt;/summary&gt;  
    public void Play()  
    {  
      if (mCanAnimate)  
      {  
        lock (image)  
        {  
          (image, new EventHandler(FrameChanged));  
        }  
      }  
    }  
 
    /// &lt;summary&gt;  
    /// Stop playback.    /// &lt;/summary&gt;  
    public void Stop()  
    {  
      if (mCanAnimate)  
      {  
        lock (image)  
        {  
          (image, new EventHandler(FrameChanged));  
        }  
      }  
    }  
 
    /// &lt;summary&gt;  
    /// Reset the animation to stop at frame 0.    /// &lt;/summary&gt;  
    public void Reset()  
    {  
      if (mCanAnimate)  
      {  
        (image, new EventHandler(FrameChanged));  
        lock (image)  
        {  
          (frameDimension, 0);  
          mCurrentFrame = 0;  
        }  
      }  
    }  
 
    private void FrameChanged(object sender, EventArgs e)  
    {  
      mCurrentFrame = mCurrentFrame + 1 &gt;= mFrameCount ? 0 : mCurrentFrame + 1;  
      lock (image)  
      {  
        (frameDimension, mCurrentFrame);  
      }  
      if (OnFrameChanged != null)  
      {  
        OnFrameChanged(image, e);  
      }  
    }  
  }  
}

Called using the following method:

using System;  
using ;  
using ;  
using ;  
using ;  
using ;  
using ;  
using ;  
 
namespace GifTest  
{  
  public partial class Form1 : Form  
  {  
    AnimateImage image;  
 
    public Form1()  
    {  
      InitializeComponent();  
      image = new AnimateImage((@"C:\Documents and Settings\Administrator\My Documents\My Pictures\Unnamed.gif"));  
       += new EventHandler&lt;EventArgs&gt;(image_OnFrameChanged);  
      SetStyle( |  | , true);  
    }  
 
    void image_OnFrameChanged(object sender, EventArgs e)  
    {  
      Invalidate();  
    }  
 
    private void Form1_Load(object sender, EventArgs e)  
    {  
      ();  
    }  
 
    private void Form1_Paint(object sender, PaintEventArgs e)  
    {  
      lock ()  
      {  
        (, new Point(0, 0));  
      }  
    }  
 
    private void button1_Click(object sender, EventArgs e)  
    {  
      if (("Stop"))  
      {  
        ();  
         = "Play";  
      }  
      else 
      {  
        ();  
         = "Stop";  
      }  
      Invalidate();  
    }  
 
    private void button2_Click(object sender, EventArgs e)  
    {  
      ();  
       = "Play";  
      Invalidate();  
    }  
  }  
}

Summarize

This is the end of this article about how to draw animations in C# Winform. For more related content on drawing animations in C# Winform, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!