首页 新闻 会员 周边

C# 使用Image 类读取gif 图片,调用Dispose 是否资源的时候提示出错

0
悬赏园豆:10 [已解决问题] 解决于 2012-12-12 17:54

就是这一张图片,我试了一下别的图片,不会出现这种情况,不知道是不是这张图片比较特殊,还是为什么

代码

//打开图片
   pictureBox1.Image = Image.FromFile(ofdGif.FileName);

//保存图片并且释放资源
  pictureBox1.Image.Save("c:\\test.gif",   System.Drawing.Imaging.ImageFormat.Gif);
            pictureBox1.Image.Dispose();
//异常 
  在 System.Drawing.Image.SelectActiveFrame(FrameDimension dimension, Int32 frameIndex)
   在 System.Drawing.ImageAnimator.ImageInfo.UpdateFrame()
   在 System.Drawing.ImageAnimator.UpdateFrames()
   在 System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
   在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   在 System.Windows.Forms.Control.WmPaint(Message& m)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   在 System.Windows.Forms.Application.Run(Form mainForm)
   在 TestWinform2.Program.Main() 位置 F:\My Documents\Visual Studio 2010\Projects\TestWinfom\TestWinform2\Program.cs:行号 29
   在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   在 System.Threading.ThreadHelper.ThreadStart()

我有试了一下其他的gif 图片,试了一张,另外一张图片不会出现这种情况,我怀疑这是.net frame work 内部出现的错误,有些gif 图片在是否资源的时候会出现这种情况,有些切不会,愁死我了,搞了半天居然没解决,mf!
极无宪的主页 极无宪 | 初学一级 | 园豆:37
提问于:2012-12-12 17:29
< >
分享
最佳答案
0

pictureBox1.Image = Image.FromFile(ofdGif.FileName);
Image img = pictureBox1.Image;

pictureBox1.Image = null;

img.Save("c:\\test.gif",   System.Drawing.Imaging.ImageFormat.Gif);
img.Dispose();

收获园豆:10
Launcher | 高人七级 |园豆:45045 | 2012-12-12 17:45

image 是引用对象,你把他设置为null ,还能save吗,我试一下

极无宪 | 园豆:37 (初学一级) | 2012-12-12 17:47

@曾先锋: 没看我把它保存到一个临时变量 img 上了吗?用img来 Save。

Launcher | 园豆:45045 (高人七级) | 2012-12-12 17:48

真的行哦,能不能解释一下

极无宪 | 园豆:37 (初学一级) | 2012-12-12 17:49

@曾先锋: 你使用了控件 pictureBox1 ,它会 Paint 你给的图片,你图片 Dispose 了后,Paint 的时候就找不到了,所以出错。

Launcher | 园豆:45045 (高人七级) | 2012-12-12 17:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册