加载带有动画GIF的PictureBox后,“GDI +中发生了一般错误”

时间:2009-12-31 00:06:48

标签: .net winforms picturebox

我在.NET 2.0中有一个Windows窗体应用程序,窗体上有一个PictureBox,我通过设置PictureBox的ImageLocation属性加载动画GIF。当动画呈现下一帧时,我得到以下异常和堆栈跟踪:

A generic error occurred in GDI+.
   at System.Drawing.Image.SelectActiveFrame(FrameDimension dimension, Int32 frameIndex)
   at System.Drawing.ImageAnimator.ImageInfo.UpdateFrame()
   at System.Drawing.ImageAnimator.UpdateFrames()
   at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at AIRNow.IMS.Mapper.MapWizard.Main() in C:\Projects\AIRNowI\IMS\UserInterface\MapWizard\MapWizard.cs:line 14
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

3 个答案:

答案 0 :(得分:2)

mcdon的答案很接近,实际上甚至是正确的。问题是,如果让PictureBoxImageLocation属性中指定的文件中加载图片,那么流可能会在执行Load()后关闭,但之前 / em>加载了所有帧(可能只加载了第一帧)。因此,如果您手动将图片加载到Image的{​​{1}}对象,并通过其Image.FromFile()属性将此对象提供给PictureBox,则可以避免这种情况。

答案 1 :(得分:0)

我不完全确定为什么会这样,但我找到了解决方法。我将PictureBox的WaitOnLoad属性设置为True。如果我将其更改为False(默认值),则可以解决问题。

答案 2 :(得分:0)

我发现了一个处理多重tiff的相关问题。我使用“pictureBox1.Load(fileName)”,然后使用pictureBox1.Image.SelectActiveFrame(FrameDimension.Page,index)。对pict​​ureBox1.Image.SelectActiveFrame的调用抛出异常“GDI +中发生一般错误”。

问题归结为图像的加载方式。

使用以下方式加载图像时

Image _myImage = Image.FromFile(fileName);

然后将其分配给pictureBox1:

pictureBox1.Image = _myImage;

以下调用正常:

pictureBox1.Image.SelectActiveFrame(FrameDimension.Page, index);