开发者

"A generic error occurred in GDI+" after loading a PictureBox with an animated GIF

I have a Windows Forms application in .NET 2.0 with a PictureBox on a form and I load it with an animated GIF by setting the PictureBox's ImageLocation property. When it is time for the animation to rende开发者_StackOverflow中文版r the next frame, I get the following exception and stack trace:

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()


mcdon's answer is close, actually even right. The thing is that if you let the PictureBox load the picture from the file named in the ImageLocation property, then the stream probably closes after the execution of Load(), but before all the frames were loaded (probably only the first frame loads). Thus you can avoid this if you load your picture manually to an Image object with Image.FromFile(), and give this object to the PictureBox through its Image property.


I'm not entirely sure why this happens, but I found a workaround. I had the PictureBox's WaitOnLoad property set to True. If I change it to False (the default) it resolves the issue.


I found a related problem dealing with multipage tiffs. I use "pictureBox1.Load(fileName)", and then pictureBox1.Image.SelectActiveFrame(FrameDimension.Page, index). The call to pictureBox1.Image.SelectActiveFrame throws an exception "A generic error occured in GDI+".

The problem comes down to how the image is loaded.

When I load the image using:

Image _myImage = Image.FromFile(fileName);

and then assign it to pictureBox1:

pictureBox1.Image = _myImage;

The following call works properly:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜