Windows 7 TextureBrush..ctor() 错误

发布于 2024-07-17 16:41:05 字数 1407 浏览 4 评论 0原文

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

异常信息


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

StackTrace 信息


位于 System.Drawing.TextureBrush..ctor(Image image, WrapModewrappMode) 在System.Windows.Forms.ControlPaint.DrawBackgroundImage(图形g,图像backgroundImage,颜色backColor,ImageLayout backgroundImageLayout,矩形边界,矩形clipRect,点scrollOffset,RightToLeft rightToLeft) 在System.Windows.Forms.Control.PaintBackground(PaintEventArgs e,矩形矩形,颜色backColor,点scrollOffset) 在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(Message&m) 在 System.Windows.Forms.Control.WndProc(Message&m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)

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

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

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

Exception 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


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.Control.OnPaintBackground(PaintEventArgs pevent)
at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)

Any ideas about why this is happening, or how I might program around it? It's just painting a standard winform with no special background.

UPDATE:
I've found that this is only an issue when the BackgroundImageLayout = ImageLayout.Tile, which is also the default. Set it to Zoom or Center, and the issue dissapears. That's pretty unsatisfactory though, because I need it to tile.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安穩 2024-07-24 16:41:05

我有类似的问题。 就我而言,我已经处理掉了加载图像的 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);

I had a similar problem. In my case I had disposed of my MemoryStream I loaded the image from.

//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);
小傻瓜 2024-07-24 16:41:05

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

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

Turns out the solution to this had to do with the PNG file itself used for the background.
I just opened it with Paint.NET and resaved it, then put it back in the project and it worked.

Not sure what changed, but it resolved the problem.

三寸金莲 2024-07-24 16:41:05

在调用 TextureBrush 类进行平铺之前,请不要丢弃该 Image 或关闭获取该 Image 的文件流对象。 否则,TextureBrush 类将抛出内存不足异常。

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

Please do not dispose the Image or close the filestream object from where you have got the Image before calling the TextureBrush class for tiling. Otherwise the TextureBrush class will throw an Out of Memory exception.

So the better way is to show the tiled Image by calling the TextureBrush Image and then close the filestream object in the Paint event of the windows form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文