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 }
重寫的方法如下
公開方法如下
新增的類如下
新增屬性如下
源碼下載:動態圖片顯示控件.zip