watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center">

代码例如以下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. //debug 引用
  10. using System.Diagnostics;
  11.  
  12. namespace CYSoft.TS.UI.StudentInfo
  13. {
  14. public partial class PicboxPlayGif : UserControl
  15. {
  16. private Image m_imgImage = null;
  17. private EventHandler m_evthdlAnimator = null;
  18. public PicboxPlayGif()
  19. {
  20. InitializeComponent();
  21. this.SetStyle(ControlStyles.UserPaint, true);
  22. this.SetStyle(ControlStyles.DoubleBuffer, true);
  23. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  24. //为托付关联一个处理方法
  25. m_evthdlAnimator = new EventHandler(OnImageAnimate);
  26. Debug.Assert(m_evthdlAnimator != null);
  27.  
  28. }
  29.  
  30. private bool isPicboxFit = true;
  31. [Description("pic 的宽和高和 真实gif的宽和高 是否要一致 ture=一致 false=不一致 ")]
  32. public bool _isPicboxFit {
  33. get { return isPicboxFit; }
  34. set { this.isPicboxFit = value;
  35. this.Invalidate();
  36. }
  37. }
  38.  
  39. private int picWidth = 0;
  40. [Description("图片宽度(假设isPicboxFit=true 这个參数无意义)")]
  41. public int _picWidth {
  42. get { return picWidth; }
  43. set {
  44. if (!isPicboxFit) {
  45. if(value!=0)
  46. {
  47. this.picWidth = value;
  48. this.Invalidate();
  49. }
  50. }
  51. }
  52. }
  53.  
  54. private int picHeight = 0;
  55. [Description("图片高度(假设isPicboxFit=true 这个參数无意义)")]
  56. public int _picHeight {
  57. get { return picHeight; }
  58. set {
  59. if (!isPicboxFit)
  60. {
  61. if (value != 0)
  62. {
  63. this.picHeight = value;
  64. this.Invalidate();
  65. }
  66. }
  67. }
  68. }
  69.  
  70. private string imagePath = "C:\\Users\\Thinkpad\\Desktop\\素材\\WaitLoading.gif";
  71. [Description("图片路径")]
  72. public string _imagePath {
  73. get { return imagePath; }
  74. set { this.imagePath = value;
  75. this.Invalidate();
  76. }
  77. }
  78.  
  79. private AA imageLayout = AA.Stretch;
  80. [Description("图片在picbox中的显示方式")]
  81. public AA _imageLayout {
  82. get { return imageLayout; }
  83. set {
  84. this.imageLayout = value;
  85. this.Invalidate();
  86. }
  87. }
  88.  
  89. public enum AA {
  90. None,
  91. Title,
  92. Center,
  93. Stretch,
  94. Zoom
  95. }
  96.  
  97. protected override void OnPaint(PaintEventArgs e)
  98. {
  99. base.OnPaint(e);
  100. if (m_imgImage != null)
  101. {
  102. UpdateImage();
  103. //不用picbox 直接输出
  104. // e.Graphics.DrawImage(m_imgImage, new Rectangle(100, 100, m_imgImage.Width, m_imgImage.Height));
  105.  
  106. //image 格式
  107. if(imageLayout==AA.None)
  108. {
  109. pic.BackgroundImageLayout = ImageLayout.None;
  110. }else if(imageLayout==AA.Title)
  111. {
  112. pic.BackgroundImageLayout = ImageLayout.Tile;
  113. }
  114. else if (imageLayout == AA.Stretch)
  115. {
  116. pic.BackgroundImageLayout = ImageLayout.Stretch;
  117. }
  118. else if (imageLayout == AA.Zoom)
  119. {
  120. pic.BackgroundImageLayout = ImageLayout.Zoom;
  121. }
  122. else {
  123. pic.BackgroundImageLayout = ImageLayout.Center;
  124. }
  125.  
  126. pic.BackgroundImage = m_imgImage;
  127. }
  128. }
  129.  
  130. protected override void OnLoad(EventArgs e)
  131. {
  132. base.OnLoad(e);
  133. //载入图片
  134. m_imgImage = Image.FromFile(imagePath);
  135. if (isPicboxFit)
  136. {
  137. this.Width = m_imgImage.Width;
  138. this.Height = m_imgImage.Height;
  139. }
  140. else
  141. {
  142. this.Width = picWidth;
  143. this.Height = picHeight;
  144. }
  145.  
  146. //pic设置
  147. pic.BackColor = Color.Transparent;
  148. pic.Dock = DockStyle.Fill;
  149.  
  150. //BeginAnimate();
  151. }
  152.  
  153. /// <summary>
  154. /// 播放或停止 动画
  155. /// </summary>
  156. /// <param name="isPlay">true=播放 false=停止</param>
  157. public void _PlayOrEndGif(bool isPlay) {
  158. if (isPlay)
  159. {
  160. BeginAnimate();
  161. }
  162. else {
  163. if (m_imgImage != null)
  164. {
  165. StopAnimate();
  166. //m_imgImage = null;
  167. }
  168. }
  169. }
  170.  
  171. //開始动画
  172. private void BeginAnimate()
  173. {
  174. if (m_imgImage == null)
  175. return;
  176.  
  177. if (ImageAnimator.CanAnimate(m_imgImage))
  178. {
  179. //当gif动画每隔一定时间后,都会变换一帧。那么就会触发一事件,
  180. //该方法就是将当前image每变换一帧时,都会调用当前这个托付所关联的方法。
  181. ImageAnimator.Animate(m_imgImage,m_evthdlAnimator);
  182. }
  183. }
  184.  
  185. //结束动画
  186. private void StopAnimate()
  187. {
  188. if (m_imgImage == null)
  189. return;
  190.  
  191. if (ImageAnimator.CanAnimate(m_imgImage))
  192. {
  193. ImageAnimator.StopAnimate(m_imgImage,m_evthdlAnimator);
  194. }
  195. }
  196.  
  197. //切换图片(帧图片)
  198. private void UpdateImage()
  199. {
  200. if (m_imgImage == null)
  201. return;
  202.  
  203. if (ImageAnimator.CanAnimate(m_imgImage))
  204. {
  205. //获得当前gif动画的下一步须要渲染的帧。当下一步不论什么对当前gif动画的操作都是对该帧进行操作)
  206. ImageAnimator.UpdateFrames(m_imgImage);
  207. }
  208. }
  209.  
  210. private void OnImageAnimate(Object sender,EventArgs e)
  211. {
  212. //使得当前这个winfor重绘,然后去调用该winform的OnPaint()方法进行重绘
  213. this.Invalidate();
  214. }
  215.  
  216. private void PicboxPlayGif_Load(object sender, EventArgs e)
  217. {
  218.  
  219. }
  220. }
  221. }

调用:

上图右边是属性设置:

播放gif:

picboxPlayGif2._PlayOrEndGif(true);

停止gif:

picboxPlayGif2._PlayOrEndGif(true);

自己定义控件 播放GIF动画的更多相关文章

  1. 自己定义控件三部曲之动画篇(七)——ObjectAnimator基本使用

    前言: 假如生活欺骗了你, 不要悲伤,不要心急! 忧郁的日子里须要镇静: 相信吧,快乐的日子终将会来临! 心儿永远向往着未来: 如今却常是忧郁. 一切都是瞬息,一切都将会过去: 而那过去了的,就会成为 ...

  2. 自己定义控件三部曲之动画篇(十三)——实现ListView Item进入动画

    前言:宝剑锋从磨砺出,梅花香自苦寒来 相关文章: <Android自己定义控件三部曲文章索引>: http://blog.csdn.net/harvic880925/article/det ...

  3. BillBoardView自己定义控件广告板轮播

    BillBoardView自己定义控件广告板轮播 GitHub地址 compile 'com.march.billboardview:billboardview:2.0.6-beta4' BillBo ...

  4. Android自己定义控件之应用程序首页轮播图

    如今基本上大多数的Android应用程序的首页都有轮播图.就是像下图这种(此图为转载的一篇博文中的图.拿来直接用了): 像这种组件我相信大多数的应用程序都会使用到,本文就是自己定义一个这种组件,能够动 ...

  5. Android自己定义控件:进度条的四种实现方式

    前三种实现方式代码出自: http://stormzhang.com/openandroid/2013/11/15/android-custom-loading/ (源代码下载)http://down ...

  6. Android自己定义控件

    今天我们来讲一下 Android中自己定义控件的介绍,在Android中, 我们一般写xml都是用的是单个的控件来完毕的 ,但是.往往在一些项目中.单个控件有时是满足不了的.故此我们能够自己定义控件 ...

  7. 自己定义控件事实上非常easy1/6

    尊重原创转载请注明:From AigeStudio(http://blog.csdn.net/aigestudio)Power by Aige 侵权必究! 炮兵镇楼 上一节我们粗略地讲了下怎样去实现我 ...

  8. android 自己定义控件

    Android自己定义View实现非常easy 继承View,重写构造函数.onDraw.(onMeasure)等函数. 假设自己定义的View须要有自己定义的属性.须要在values下建立attrs ...

  9. Android 实现形态各异的双向側滑菜单 自己定义控件来袭

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/39670935.本文出自:[张鸿洋的博客] 1.概述 关于自己定义控件側滑已经写了 ...

随机推荐

  1. 在实现栈中原来功能的基础上,获得一个栈中最小值,要求时间复杂度 bigO(1)

    思路: 准备两个栈 stackData stackMin package my_basic; import java.util.Stack; public class GetMinStack { St ...

  2. ios 之sqllite

    iOS sqlite数据库操作.步骤是: 先加入sqlite开发库libsqlite3.dylib, 新建或打开数据库, 创建数据表, 插入数据, 查询数据并打印 1.新建项目sqliteDemo,添 ...

  3. 客户端和服务器最多能发送和接收多少TCP连接数?

    1. 对于服务器,每一个tcp连接都要占一个文件描述符,一旦这个文件描述符使用完了,就会返回错误. 我们知道操作系统上端口号1024以下是系统保留的,从1024-65535是用户使用的.由于每个TCP ...

  4. InnoDB INFORMATION_SCHEMA Temporary Table Info Table

    InnoDB INFORMATION_SCHEMA Temporary Table Info Table INNODB_TEMP_TABLE_INFO提供有关InnoDB实例中当前活动的用户创建的In ...

  5. GO:interface

    一.感受接口 type Usb interface { Connect() Disconnect() } // 手机 type Phone struct {} // 相机 type Camera st ...

  6. 【Linux】Centos6的iptables防火墙设置

    1,查看防火墙状态 # service iptables status //或 # /etc/init.d/iptables status 2,防火墙的启动.重启,关闭 # service iptab ...

  7. 条款21:必须返回对象时,别妄想返回其reference(Don't try to return a reference when you must return an object)

    NOTE: 1.绝不要返回pointer或reference 指向一个local stack 对象,或返回reference 指向一个heap-allocated对象,或返回pointer 或refe ...

  8. [第一波模拟\day3\T2]{独立集}(bubble.cpp)

    [问题描述] 有一天,一个名叫顺旺基的程序员从石头里诞生了.又有一天,他学会了冒泡排序和独立集.在一个图里,独立集就是一个点集,满足任意两个点之间没有边.于是他就想把这两个东西结合在一起.众所周知,独 ...

  9. SpringMVC修改功能

    articleList.jsp <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" ...

  10. hdu3376 Matrix Again

    最大费用最大流 咋写?取个相反数就可以了-- #include <iostream> #include <cstring> #include <cstdio> #i ...