3

我在 .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()
4

3 回答 3

2

麦克登的答案很接近,实际上甚至是正确的。问题是,如果您让PictureBox从属性中命名的文件中加载图片ImageLocation,则流可能会在执行之后关闭Load(),但加载所有帧之前(可能只加载第一帧)。因此,如果您使用 手动将图片加载到Image对象,并通过其属性Image.FromFile()将此对象赋予 ,则可以避免这种情况。PictureBoxImage

于 2010-06-09T15:47:59.410 回答
0

我不完全确定为什么会发生这种情况,但我找到了一种解决方法。我将 PictureBox 的 WaitOnLoad 属性设置为 True。如果我将其更改为 False (默认值),它将解决问题。

于 2009-12-31T00:06:57.637 回答
0

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

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

当我使用加载图像时:

Image _myImage = Image.FromFile(fileName);

然后将其分配给pictureBox1:

pictureBox1.Image = _myImage;

以下调用正常工作:

pictureBox1.Image.SelectActiveFrame(FrameDimension.Page, index);
于 2010-04-27T18:26:19.273 回答