« Une erreur générique est produite dans GDI + » après le chargement d'un PictureBox avec un GIF animé

StackOverflow https://stackoverflow.com/questions/1983223

  •  22-09-2019
  •  | 
  •  

Question

J'ai une application Windows Forms .NET 2.0 avec un PictureBox sur un formulaire et je charge avec un GIF animé en définissant la propriété de ImageLocation du PictureBox. Quand il est temps pour l'animation pour rendre l'image suivante, je reçois l'exception suivante et trace pile:

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()
Était-ce utile?

La solution 2

Je ne suis pas tout à fait sûr pourquoi cela se produit, mais j'ai trouvé une solution de contournement. J'ai eu la propriété WaitOnLoad du PictureBox définie sur True. Si je change à False (par défaut), il résout le problème.

Autres conseils

La réponse de McDon est proche, en fait même droit. La chose est que si vous laissez la PictureBox charger l'image du fichier nommé dans la propriété ImageLocation, le flux se ferme probablement après l'exécution de Load(), mais avant tous les cadres ont été chargés (probablement seulement premières charges de trame). Ainsi, vous pouvez éviter cela si vous chargez votre image manuellement à un objet Image avec Image.FromFile(), et donner cet objet à la PictureBox par sa propriété Image.

J'ai trouvé un problème connexe traitant tiff multipages. J'utilise "pictureBox1.Load (fileName)", puis pictureBox1.Image.SelectActiveFrame (FrameDimension.Page, index). L'appel à pictureBox1.Image.SelectActiveFrame lance une exception « Une erreur générique est produite dans GDI + ».

Le problème se résume à la façon dont l'image est chargée.

Quand je charge l'image en utilisant:

Image _myImage = Image.FromFile(fileName);

et puis l'affecter à pictureBox1:

pictureBox1.Image = _myImage;

L'appel suivant fonctionne correctement:

pictureBox1.Image.SelectActiveFrame(FrameDimension.Page, index);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top