代码之家  ›  专栏  ›  技术社区  ›  Jon Dewees

Windows 7 TextureBrush..ctor()错误

  •  5
  • Jon Dewees  · 技术社区  · 15 年前

    我有一个.NET 2.0应用程序在XP和Vista上运行得很好,但在Windows 7 RC(x64)上,它会崩溃,并出现以下错误:

    异常信息


    异常类型:System.OutOfMemoryException 信息:内存不足。 数据:system.collections.listDictionaryInternal 目标站点:void.ctor(system.drawing.image,system.drawing.drawing2d.wrapmode) Helpink:零 资料来源:系统图

    StackTrace信息


    在System.Drawing.TextureBrush.ctor(图像、包装模式包装模式) 在system.windows.forms.controlpaint.brundergroundimage(图形G、图像背景图像、颜色背景色、图像布局背景图像布局、矩形边界、矩形剪贴画、点滚动偏移、righttoleft righttoleft) 在system.windows.forms.control.paintbackground(paintEventArgs e,矩形矩形,颜色背景色,点滚动偏移) 在system.windows.forms.control.paintbackground(paintEventArgs e,矩形矩形) 在system.windows.forms.control.onpaintbackground(paintEventargs pevent)上 在system.windows.forms.scrollablecontrol.onpaintbackground(paintEventargs e)上 在system.windows.forms.control.paintwithorrHandling上(paintEventArgs e,int16层,boolean disposeEventArgs) at system.windows.forms.control.wmpaint(消息&m) at system.windows.forms.control.wndproc(消息&m) 在System.Windows.Forms.ScrollableControl.WndProc(消息&M)

    有什么关于这是为什么发生的,或者我如何围绕它进行编程的想法吗?它只是画一个没有特殊背景的标准WinForm。

    更新: 我发现这只是backgroundImageLayout=ImageLayout.tile时的问题,它也是默认值。将其设置为缩放或居中,问题将不模拟。不过,这并不令人满意,因为我需要瓷砖。

    3 回复  |  直到 15 年前
        1
  •  3
  •   Ben Lesh    15 年前

    我也有类似的问题。在我的情况下,我已经处理了我从中加载图像的内存流。

    //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);
    
        2
  •  1
  •   Jon Dewees    15 年前

    结果证明,解决这个问题的方法与用于背景的PNG文件本身有关。 我刚用paint.net打开它并重新保存,然后把它放回项目中,它就工作了。

    不知道发生了什么变化,但它解决了问题。

        3
  •  1
  •   Soumiek Ranjan Chakravarty    15 年前

    在调用TextureBrush类进行平铺之前,请不要释放图像或关闭从中获取图像的文件流对象。否则,TextureBrush类将抛出内存不足异常。

    因此,更好的方法是通过调用texturebrush图像来显示平铺图像,然后在windows窗体的paint事件中关闭filestream对象。