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.
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));
}
}
}
}
Are there any ideas on what is wrong with the code? Or could it be a problem with GDI+?