Windows 7 Error TextureBrush..ctor ()

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

Exclusion 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


in System.Drawing.TextureBrush..ctor (Image Image, WrapMode wrapMode) in System.Windows.Forms.ControlPaint.DrawBackgroundImage (Graphics g, Image backgroundImage, Color backColor, ImageLayout backgroundImageLayout, Rectangle bounds, Rectangle clipRect, Point scrollOffset, RightToLeft Right ) in System.Windows.Forms.Control.PaintBackground (PaintEventArgs e, Rectangle Rectangle, Color backColor, Point scrollOffset) in System.Windows.Forms.Control.PaintBackground (PaintEventArgs e, rectangle rectangle) in System.Windows.Forms.Control. OnPaintBackground (PaintEventArgs pevent) in System.Windows.Forms.ScrollableControl.OnPaintBackground (PaintEventArgs e) in System.Windows.Forms.Control.PaintWithErrorHandling (PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) in System.Windows. (Message & m) in System.Windows.Forms.Control.WndProc (Message & m) in System.Windows.Forms.ScrollableControl.WndProc (Message & m)

, , , ? winform .

UPDATE: , , BackgroundImageLayout = ImageLayout.Tile, . Zoom Center, . , , .

+5
3

, PNG, . Paint.NET , .

, , .

+1

. MemoryStream, .

//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 right it 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);
+3

Please do not delete the image or close the stream object where you received the image from before calling the TextureBrush class for tiling. Otherwise, the TextureBrush class will throw an exception in memory.

So, the best way is to display the tiled image by calling the TextureBrush image, and then close the filestream object in the Paint event in the form of a window.

+1
source

All Articles