【问题标题】:Windows 7 TextureBrush..ctor() errorWindows 7 TextureBrush..ctor() 错误
【发布时间】:2010-02-15 13:03:41
【问题描述】:

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

异常信息


异常类型:System.OutOfMemoryException 消息:内存不足。 数据:System.Collections.ListDictionaryInternal TargetSite: Void .ctor(System.Drawing.Image, System.Drawing.Drawing2D.WrapMode) 帮助链接:NULL 来源:System.Drawing

堆栈跟踪信息


在 System.Drawing.TextureBrush..ctor(Image image, WrapMode wrapMode) 在 System.Windows.Forms.ControlPaint.DrawBackgroundImage(图形 g,图像背景图像,颜色背景色,图像布局背景图像布局,矩形边界,矩形 clipRect,点滚动偏移,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.PaintWithErrorHandling(PaintEventArgs e,Int16 层,布尔 disposeEventArgs) 在 System.Windows.Forms.Control.WmPaint(消息和 m) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message&m)

关于为什么会发生这种情况的任何想法,或者我可以如何围绕它进行编程?它只是绘制一个没有特殊背景的标准winform。

更新: 我发现这只是当 BackgroundImageLayout = ImageLayout.Tile 时的问题,这也是默认设置。将其设置为缩放或居中,问题就会消失。不过这很不令人满意,因为我需要它来平铺。

【问题讨论】:

  • 在 XP 和 Vista 64 位上可以正常工作吗?
  • 是的,它适用于 32 位和 64 位版本的 XP 和 Vista。
  • 谢谢(这是在黑暗中拍摄,最近遇到了与跨拱问题相关的类似问题。)抱歉,没有想法。

标签: windows-7 .net-2.0 crash


【解决方案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'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);

【讨论】:

  • 这正是我的问题。了解“幕后”.net 的人会费心解释这种行为吗?
  • 刚刚碰到这个。我第二个@Dinei,有人明白为什么会这样吗?我也很好奇。
【解决方案2】:

原来这个问题的解决方案与用于背景的 PNG 文件本身有关。 我只是用 Paint.NET 打开它并重新保存它,然后将它放回项目中,它就可以工作了。

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

【讨论】:

    【解决方案3】:

    在调用 TextureBrush 类进行平铺之前,请不要释放 Image 或关闭获取 Image 的文件流对象。否则 TextureBrush 类将抛出 Out of Memory 异常。

    所以更好的方法是通过调用TextureBrush Image来显示平铺的Image,然后在windows窗体的Paint事件中关闭filestream对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 2017-05-14
      • 2011-04-25
      相关资源
      最近更新 更多