Question

I have a .NET 2.0 app that runs just fine on XP and Vista, but on Windows 7 RC (x64) it crashes with the following error:

Exception Information


Exception Type: System.OutOfMemoryException Message: Out of memory. Data: System.Collections.ListDictionaryInternal TargetSite: Void .ctor(System.Drawing.Image, System.Drawing.Drawing2D.WrapMode) HelpLink: NULL Source: System.Drawing

StackTrace Information


at System.Drawing.TextureBrush..ctor(Image image, WrapMode wrapMode) at System.Windows.Forms.ControlPaint.DrawBackgroundImage(Graphics g, Image backgroundImage, Color backColor, ImageLayout backgroundImageLayout, Rectangle bounds, Rectangle clipRect, Point scrollOffset, RightToLeft rightToLeft) at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset) at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle) at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent) at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e) 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.ScrollableControl.WndProc(Message& m)

Any ideas about why this is happening, or how I might program around it? It's just painting a standard winform with no special background.

UPDATE: I've found that this is only an issue when the BackgroundImageLayout = ImageLayout.Tile, which is also the default. Set it to Zoom or Center, and the issue dissapears. That's pretty unsatisfactory though, because I need it to tile.

Was it helpful?

Solution 2

Turns out the solution to this had to do with the PNG file itself used for the background. I just opened it with Paint.NET and resaved it, then put it back in the project and it worked.

Not sure what changed, but it resolved the problem.

OTHER TIPS

I had a similar problem. In my case I had disposed of my MemoryStream I loaded the image from.

//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);

Please do not dispose the Image or close the filestream object from where you have got the Image before calling the TextureBrush class for tiling. Otherwise the TextureBrush class will throw an Out of Memory exception.

So the better way is to show the tiled Image by calling the TextureBrush Image and then close the filestream object in the Paint event of the windows form.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top