用户登录
用户注册

分享至

C# Winform中如何绘制动画示例详解

  • 作者: 汁多腥--灭绝
  • 来源: 51数据库
  • 2022-04-29

前言

这里介绍一个.net自身携带的类imageanimator,这个类类似于控制动画的时间轴,使用imageanimator.cananimate可以判断一个图片是否为动画,调用imageanimator.animate可以开始播放动画,即每经过一帧的时间触发一次onframechanged委托,我们只要在该委托中将image的活动帧选至下一帧再迫使界面重绘就可以实现动画效果了。

为了方便以后的使用,我将这些代码整合到了一起,形成一个animateimage类,该类提供了cananimate、framecount、currentframe等属性,以及play()、stop()、reset()等动画常用的方法,代码如下

using system;  
using system.collections.generic;  
using system.text;  
using system.drawing;  
using system.drawing.imaging;  
 
namespace giftest  
{  
  /// <summary>  
  /// 表示一类带动画功能的图像。  
  /// </summary>  
  public class animateimage  
  {  
    image image;  
    framedimension framedimension;  
    /// <summary>  
    /// 动画当前帧发生改变时触发。  
    /// </summary>  
    public event eventhandler<eventargs> onframechanged;  
 
    /// <summary>  
    /// 实例化一个animateimage。  
    /// </summary>  
    /// <param name="img">动画图片。</param>  
    public animateimage(image img)  
    {  
      image = img;  
 
      lock (image)  
      {  
        mcananimate = imageanimator.cananimate(image);  
        if (mcananimate)  
        {  
          guid[] guid = image.framedimensionslist;  
          framedimension = new framedimension(guid[0]);  
          mframecount = image.getframecount(framedimension);  
        }  
      }  
    }  
 
    bool mcananimate;  
    int mframecount = 1, mcurrentframe = 0;  
 
    /// <summary>  
    /// 图片。  
    /// </summary>  
    public image image  
    {  
      get { return image; }  
    }  
 
    /// <summary>  
    /// 是否动画。  
    /// </summary>  
    public bool cananimate  
    {  
      get { return mcananimate; }  
    }  
 
    /// <summary>  
    /// 总帧数。  
    /// </summary>  
    public int framecount  
    {  
      get { return mframecount; }  
    }  
 
    /// <summary>  
    /// 播放的当前帧。  
    /// </summary>  
    public int currentframe  
    {  
      get { return mcurrentframe; }  
    }  
 
    /// <summary>  
    /// 播放这个动画。  
    /// </summary>  
    public void play()  
    {  
      if (mcananimate)  
      {  
        lock (image)  
        {  
          imageanimator.animate(image, new eventhandler(framechanged));  
        }  
      }  
    }  
 
    /// <summary>  
    /// 停止播放。  
    /// </summary>  
    public void stop()  
    {  
      if (mcananimate)  
      {  
        lock (image)  
        {  
          imageanimator.stopanimate(image, new eventhandler(framechanged));  
        }  
      }  
    }  
 
    /// <summary>  
    /// 重置动画,使之停止在第0帧位置上。  
    /// </summary>  
    public void reset()  
    {  
      if (mcananimate)  
      {  
        imageanimator.stopanimate(image, new eventhandler(framechanged));  
        lock (image)  
        {  
          image.selectactiveframe(framedimension, 0);  
          mcurrentframe = 0;  
        }  
      }  
    }  
 
    private void framechanged(object sender, eventargs e)  
    {  
      mcurrentframe = mcurrentframe + 1 >= mframecount ? 0 : mcurrentframe + 1;  
      lock (image)  
      {  
        image.selectactiveframe(framedimension, mcurrentframe);  
      }  
      if (onframechanged != null)  
      {  
        onframechanged(image, e);  
      }  
    }  
  }  
}

使用如下方法调用:

using system;  
using system.collections.generic;  
using system.componentmodel;  
using system.data;  
using system.drawing;  
using system.drawing.imaging;  
using system.text;  
using system.windows.forms;  
 
namespace giftest  
{  
  public partial class form1 : form  
  {  
    animateimage image;  
 
    public form1()  
    {  
      initializecomponent();  
      image = new animateimage(image.fromfile(@"c:\documents and settings\administrator\my documents\my pictures\未命名.gif"));  
      image.onframechanged += new eventhandler<eventargs>(image_onframechanged);  
      setstyle(controlstyles.optimizeddoublebuffer | controlstyles.allpaintinginwmpaint | controlstyles.userpaint, true);  
    }  
 
    void image_onframechanged(object sender, eventargs e)  
    {  
      invalidate();  
    }  
 
    private void form1_load(object sender, eventargs e)  
    {  
      image.play();  
    }  
 
    private void form1_paint(object sender, painteventargs e)  
    {  
      lock (image.image)  
      {  
        e.graphics.drawimage(image.image, new point(0, 0));  
      }  
    }  
 
    private void button1_click(object sender, eventargs e)  
    {  
      if (button1.text.equals("stop"))  
      {  
        image.stop();  
        button1.text = "play";  
      }  
      else 
      {  
        image.play();  
        button1.text = "stop";  
      }  
      invalidate();  
    }  
 
    private void button2_click(object sender, eventargs e)  
    {  
      image.reset();  
      button1.text = "play";  
      invalidate();  
    }  
  }  
}

总结

到此这篇关于c# winform中如何绘制动画的文章就介绍到这了,更多相关c# winform绘制动画内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

软件
前端设计
程序设计
Java相关