动态图片显示控件----------WinForm控件开发系列
PictureBox 控件无法显示gif格式的图片,该控件是继承于 Control 基类开发的。该控件利用.NET自带 ImageAnimator 类来处理图片的帧。如下:
1 /// <summary> 2 /// 开始循环播放动态图片 3 /// </summary> 4 private void StartAnimation() 5 { 6 lock (this.Image) 7 { 8 ImageAnimator.Animate(this.Image, new EventHandler(this.FrameChanged)); 9 10 11 if (!this.DesignMode) 12 { 13 this.OnFrameIndexChanged(new FrameIndexChangedEventArgs() { FrameIndex = currentFrame }); 14 } 15 } 16 } 17 18 /// <summary> 19 /// 停止循环播放动态图片 20 /// </summary> 21 private void StopAnimation() 22 { 23 lock (this.Image) 24 { 25 ImageAnimator.StopAnimate(this.Image, new EventHandler(this.FrameChanged)); 26 this.resetProperty(); 27 28 if (!this.DesignMode) 29 { 30 this.OnFrameIndexChanged(new FrameIndexChangedEventArgs() { FrameIndex = currentFrame }); 31 } 32 } 33 } 34 35 /// <summary> 36 /// 重置图片信息 37 /// </summary> 38 private void resetProperty() 39 { 40 this.frameDimension = null; 41 this.isAnimation = false; 42 this.frameCount = 0; 43 this.currentFrame = -1; 44 } 45 46 /// <summary> 47 /// 当前帧更改事件 48 /// </summary> 49 /// <param name="sender"></param> 50 /// <param name="e"></param> 51 private void FrameChanged(object sender, EventArgs e) 52 { 53 this.currentFrame = this.currentFrame + 1 >= this.frameCount ? 0 : this.currentFrame + 1; 54 lock (this.image) 55 { 56 this.image.SelectActiveFrame(this.frameDimension, this.currentFrame); 57 this.Invalidate(); 58 } 59 }
重写的方法如下
公开方法如下
新增的类如下
新增属性如下