我有一个.NET 2.0应用程序,在XP和Vista上运行得很好,但在Windows 7 RC(x64)上崩溃时出现以下错误:
异常信息
异常类型:System.OutOfMemoryException 消息:内存不足。 数据:System.Collections.ListDictionaryInternal TargetSite:Void .ctor(System.Drawing.Image,System.Drawing.Drawing2D.WrapMode) HelpLink:NULL 来源:System.Drawing
StackTrace信息
在System.Drawing.TextureBrush..ctor(Image image,WrapMode wrapMode) 在System.Windows.Forms.ControlPaint.DrawBackgroundImage(Graphics g,Image backgroundImage,Color backColor,ImageLayout backgroundImageLayout,Rectangle bounds,Rectangle clipRect,Point scrollOffset,RightToLeft rightToLeft) 在System.Windows.Forms.Control.PaintBackground(PaintEventArgs e,Rectangle矩形,Color backColor,Point scrollOffset) 在System.Windows.Forms.Control.PaintBackground(PaintEventArgs e,Rectangle矩形) 在System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent) 在System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e) 在System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16 layer,Boolean disposeEventArgs) 在System.Windows.Forms.Control.WmPaint(消息& m) 在System.Windows.Forms.Control.WndProc(消息& m) 在System.Windows.Forms.ScrollableControl.WndProc(消息& m)
关于为什么会发生这种情况的任何想法,或者我如何围绕它进行编程?它只是绘制一个没有特殊背景的标准winform。
更新: 我发现这只是BackgroundImageLayout = ImageLayout.Tile时的一个问题,它也是默认值。将其设置为缩放或中心,问题消失。但这非常令人不满意,因为我需要它来平铺。
答案 0 :(得分:4)
我有类似的问题。在我的情况下,我已经处理了我的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 :(得分:1)
原来解决这个问题的方法与用于后台的PNG文件本身有关。 我刚用Paint.NET打开它并重新保存它,然后把它放回到项目中并且它有效。
不确定发生了什么变化,但它解决了问题。
答案 2 :(得分:1)
在调用TextureBrush类进行平铺之前,请不要处理Image或从您拥有Image的位置关闭filestream对象。否则,TextureBrush类将抛出Out of Memory异常。
因此,更好的方法是通过调用TextureBrush Image显示平铺的Image,然后在Windows窗体的Paint事件中关闭filestream对象。