C#动画处理,gif老是无法正常显示
在做图片软件的时候,遇到gif无法正常显示,这是显示窗体的代码程序代码:
using System.Drawing; using System.Windows.Forms; using System; namespace WindowsFormsApplication1 { class F:Form { Bitmap bitmap; //是否为动画,用于ImageAnimator.Stop判断 bool IsGif = false; private void OnFrameChanged(object o, EventArgs e) { ImageAnimator.UpdateFrames(); this.CreateGraphics().DrawImage(this.bitmap, 0, 0); } //传入图片 public void ShowImg(Bitmap bt) { bitmap = bt; //窗体设置 InitializeComponent(); this.ShowDialog(); } //绘制 protected override void OnPaint(PaintEventArgs e) { if (ImageAnimator.CanAnimate(bitmap)) { //静态图片处理 ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged)); IsGif = true; } else { //动态图片处理 this.CreateGraphics().DrawImage(this.bitmap, 0, 0); IsGif = false; } } private void InitializeComponent() { this.SuspendLayout(); // // F // this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Width = bitmap.Width + 16; this.Height = bitmap.Height + 38; this.ShowIcon = false; this.MaximizeBox = false; // this.ClientSize = new System.Drawing.Size(284, 262); this.Name = "F"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.F_FormClosing); this.ResumeLayout(false); } //关闭窗体时,释放资源 private void F_FormClosing(object sender, FormClosingEventArgs e) { if (IsGif) ImageAnimator.StopAnimate(bitmap, new EventHandler(this.OnFrameChanged)); bitmap.Dispose(); } } }[local]2[/local]在做图片软件的时候,遇到gif无法正常显示,这是显示窗体的代码
程序代码:
using System.Drawing; using System.Windows.Forms; using System; namespace WindowsFormsApplication1 { class F:Form { Bitmap bitmap; //是否为动画,用于ImageAnimator.Stop判断 bool IsGif = false; private void OnFrameChanged(object o, EventArgs e) { ImageAnimator.UpdateFrames(); this.CreateGraphics().DrawImage(this.bitmap, 0, 0); } //传入图片 public void ShowImg(Bitmap bt) { bitmap = bt; //窗体设置 InitializeComponent(); this.ShowDialog(); } //绘制 protected override void OnPaint(PaintEventArgs e) { if (ImageAnimator.CanAnimate(bitmap)) { //静态图片处理 ImageAnimator.Animate(bitmap, new EventHandler(this.OnFrameChanged)); IsGif = true; } else { //动态图片处理 this.CreateGraphics().DrawImage(this.bitmap, 0, 0); IsGif = false; } } private void InitializeComponent() { this.SuspendLayout(); // // F // this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.Width = bitmap.Width + 16; this.Height = bitmap.Height + 38; this.ShowIcon = false; this.MaximizeBox = false; // this.ClientSize = new System.Drawing.Size(284, 262); this.Name = "F"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.F_FormClosing); this.ResumeLayout(false); } //关闭窗体时,释放资源 private void F_FormClosing(object sender, FormClosingEventArgs e) { if (IsGif) ImageAnimator.StopAnimate(bitmap, new EventHandler(this.OnFrameChanged)); bitmap.Dispose(); } } }[local]2[/local]