Question

J'ai une application .NET 2.0 qui fonctionne très bien sous XP et Vista, mais elle se bloque sous Windows 7 RC (x64) avec l'erreur suivante:

Informations sur les exceptions

Type d'exception: System.OutOfMemoryException Message: Mémoire insuffisante. Données: System.Collections.ListDictionaryInternal TargetSite: Annulation .ctor (System.Drawing.Image, System.Drawing.Drawing2D.WrapMode) HelpLink: NULL Source: System.Drawing

Informations StackTrace

sur System.Drawing.TextureBrush..ctor (Image image, WrapMode wrapMode) sur System.Windows.Forms.ControlPaint.DrawBackgroundImage (Graphics g, Image de fond, Couleur de fond, Couleur arrière-plan, ImageLayout arrière-planImageLayout, Limites du rectangle, Rectangle clipRect, Point scrollOffset, RightToLeft rightToLeft) à System.Windows.Forms.Control.PaintBackground (PaintEventArgs, Rectangle rectangle, Couleur backColor, Point scrollOffset) sur System.Windows.Forms.Control.PaintBackground (PaintEventArgs e, Rectangle rectangle) sur System.Windows.Forms.Control.OnPaintBackground (PaintEventArgs pevent) sur System.Windows.Forms.ScrollableControl.OnPaintBackground (PaintEventArgs e) sur System.Windows.Forms.Control.PaintWithErrorHandling (PaintEventArgs e, couche Int16, Boolean disposeEventArgs) sur System.Windows.Forms.Control.WmPaint (Message) sur System.Windows.Forms.Control.WndProc (Message) sur System.Windows.Forms.ScrollableControl.WndProc (Message)

Avez-vous des idées sur les raisons pour lesquelles cela se produit ou sur la manière dont je pourrais programmer autour de cela? Il s’agit simplement de peindre un winform standard sans arrière-plan particulier.

UPDATE: J'ai constaté qu'il ne s'agissait que d'un problème lorsque BackgroundImageLayout = ImageLayout.Tile, qui est également la valeur par défaut. Réglez-le sur Zoom ou Centre, et le problème disparaît. C’est plutôt insatisfaisant, car j’en ai besoin de mosaïque.

Était-ce utile?

La solution 2

La solution à ce problème concerne le fichier PNG lui-même utilisé pour l’arrière-plan. Je viens juste de l'ouvrir avec Paint.NET et de le réenregistrer, puis de le replacer dans le projet et cela a fonctionné.

Je ne sais pas ce qui a changé, mais le problème a été résolu.

Autres conseils

J'ai eu un problème similaire. Dans mon cas, j’avais jeté mon MemoryStream dont je chargeais l’image.

//The following throws and OutOfMemoryException at the TextureBrush.ctor():

    /*someBytes and g declared somewhere up here*/
    Bitmap myBmp = null;
    using(MemoryStream ms = new MemoryStream(someBytes))
       myBmp = new Bitmap(ms);

    if(myBmp != null) //that's right it's not null.
       using(TextureBrush tb = new TextureBrush(myBmp)) //OutOfMemoryException thrown
          g.FillRectangle(tb,0,0,50,50);

//This code does not throw the same error:

    /*someBytes and g declared somewhere up here*/
        MemoryStream ms = new MemoryStream(someBytes);
        Bitmap myBmp = new Bitmap(ms);

        if(myBmp != null)
           using(TextureBrush tb = new TextureBrush(myBmp))
              g.FillRectangle(tb,0,0,50,50);

Veuillez ne pas supprimer l’image ni fermer l’objet filestream à partir duquel vous avez obtenu l’image avant d’appeler la classe TextureBrush pour la mosaïque. Sinon, la classe TextureBrush lève une exception de mémoire insuffisante.

Le meilleur moyen consiste donc à afficher l'image en mosaïque en appelant l'image TextureBrush, puis à fermer l'objet filestream dans l'événement Paint du formulaire windows.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top