2009-04-30 43 views
5

我有一個運行在XP和Vista就好了一個.NET 2.0的應用程序,但在Windows 7 RC(64位),它與下面的錯誤崩潰:Windows 7的TextureBrush..ctor()錯誤

異常信息


異常類型:的System.OutOfMemoryException 消息:內存。 數據:System.Collections.ListDictionaryInternal TargetSite:空隙.ctor(System.Drawing.Image對象,System.Drawing.Drawing2D.WrapMode) HELPLINK:NULL 來源:System.Drawing中

堆棧跟蹤信息


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。在System.Windows.Forms的System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16圖層,布爾disposeEventArgs)上的System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e) .Control.WmPaint(消息&米) 在System.Windows.Forms.Control.WndProc(消息&米) 在System.Windows.Forms.ScrollableControl.WndProc(消息&米)

有關爲什麼會發生這種情況的任何想法,或者我如何編程?這只是繪製一個沒有特殊背景的標準winform。

更新: 我發現這只是一個問題,當BackgroundImageLayout = ImageLayout.Tile,這也是默認的。將其設置爲「縮放」或「居中」,問題消失。雖然這很不理想,因爲我需要它來平鋪。

+0

它可以在XP和Vista ** 64位**上正常工作嗎? – 2009-04-30 21:13:48

+0

是的,它在32位和64位版本的XP和Vista上都能正常工作。 – 2009-04-30 21:16:03

+0

謝謝(這是在黑暗中拍攝的,最近出現了一些與cross-arch問題相似的問題。)對不起,沒有想法。 – 2009-04-30 21:21:38

回答

1

原來這個解決方案與用於背景的PNG文件本身有關。 我剛剛用Paint.NET打開它並重新保存它,然後將它放回到項目中並且它工作正常。

不知道什麼改變了,但它解決了這個問題。

3

我有一個類似的問題。在我的情況下,我已經處理了我的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'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); 
1

請勿將圖像或調用的TextureBrush類貼磚之前關閉,從那裏你已經拿到了圖像的文件流對象。否則,TextureBrush類將拋出一個Out of Memory異常。

所以更好的方法是通過調用TextureBrush圖像來顯示平鋪圖像,然後在窗體的Paint事件中關閉文件流對象。