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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With