当递归绘制在GDI矩形内存不足异常+ [英] OutOfMemory Exception when recursively drawing rectangles in GDI+

查看:126
本文介绍了当递归绘制在GDI矩形内存不足异常+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,图纸和使用GDI + C#中填充矩形。我试图呈现一个树形并因此构建了一个递归算法从遍历树状结构根至叶的水平和绘制一个矩形,在任何情况下,还填充矩形如果节点恰好是一个叶节点。

I have a problem drawing and filling rectangles in C# using GDI+. I'm trying to render a treemap and have therefore constructed a recursive algorithm which traverses the tree structure from root to leaf levels and draws a rectangle in any case, but also fills the rectangle if the node happens to be a leaf node.

在code正常工作对较小的树木,但reproducably崩溃的有42层节点的更大的数据集。上的DrawRectangle 调用抛出 OutOfMemoryException异常试图使16层以下的一个节点,独立的32/64时,比特或调试和发布配置。

The code works fine for smaller trees, but reproducably crashes for a larger data set which has 42 layers of nodes. The upper DrawRectangle call throws an OutOfMemoryException when trying to render a node below the 16th layer, independent of 32-/64-bit or debug and release configurations.

毛刷和笔不受改变,所以它们被存储在该方法以外的阵列。没有与创建或处理的对象没有问题的。

Brushes and Pens are not subject to change, so they are stored in an array outside the method. There is no problem with creating or disposing objects.

下面是我用它来呈现树形递归方法:

Here is the recursive method I use to render the treemap:

/// <summary>
/// Renders a certain <see cref="Tree"/> onto the canvas.
/// </summary>
/// <param name="tree">The tree node to be rendered.</param>
/// <param name="g">The <see cref="Graphics"/> canvas to render the tree to.</param>
/// <param name="bounds">The rectangle available for the specified <paramref name="tree"/>.</param>
protected void RenderTree(Tree tree, Graphics g, RectangleF bounds)
{
    if (tree.IsLeaf)
    {
        g.FillRectangle(brushes[tree.Depth], bounds);
        g.DrawRectangle(pens[tree.Depth], bounds);
    }
    else
    {
        g.DrawRectangle(pens[tree.Depth], bounds);

        if (bounds.Width >= bounds.Height)
        {
            float widthPerChild = bounds.Width / (float)tree.Children.Count;
            for (int i = 0; i < tree.Children.Count; ++i)
            {
                RenderTree(tree.Children[i], g, new RectangleF(bounds.X + i * widthPerChild, bounds.Y, widthPerChild, bounds.Height));
            }
        }
        else
        {
            float heightPerChild = bounds.Height / (float)tree.Children.Count;
            for (int i = 0; i < tree.Children.Count; ++i)
            {
                RenderTree(tree.Children[i], g, new RectangleF(bounds.X, bounds.Y + i * heightPerChild, bounds.Width, heightPerChild));
            }
        }
    }

}

有没有对什么是错的code什么想法?或可能是用GDI有问题+?

Are there any ideas on what is wrong with the code? Or could it be a problem with GDI+?

推荐答案

感谢您所有的意见;我可以解决这个问题。我还测试一个迭代版本的拉丝工艺,它并没有改变什么,但给了我更好的调试选项。 这个问题似乎已经是我试图绘制矩形太小(宽度和高度> 0,但大约10 ^( - 5)):绘制矩形前,我添加如果阈值矩形的宽度或高度小于1/1000。当然,这使一个为无的内存问题,你讲话称显示任何可疑行为 OutOfMemoryException异常不是非常有帮助。

Thank you for all the comments; I could solve the problem. I also tested an iterative version of the drawing process and it did not change anything but gave me better debugging options. The problem seems to have been that the rectangles I tried to draw were too small (width and height > 0, but around 10^(-5)): Before drawing the rectangles, I added a threshold if the width or height of the rectangle is less than 1/1000. Of course, this makes an OutOfMemoryException not very helpful as none of the memory issues you remarked showed any suspicious behavior.

这篇关于当递归绘制在GDI矩形内存不足异常+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆