C# – WPF: How to get the true size (Bounding Box) of shapes

cnetwpf

I'm having trouble with getting the actual size (bounding box) of Shapes.

I tried to use both RenderSize and ActualSize but they return values that don't make sense.
However, using these methods for UIElements works great.

if you can help me with this I will be greateful.

Best Answer

You can get the Bounding Box for any Visual using TransformToVisual

So if you have a Polygon defined like this

<Canvas Name="canvas">
    <Polygon Name="polygon" Canvas.Left="12" Canvas.Top="12"
             Points="0,75 100,0 100,150 0,75"
             Stroke="Purple"
             Fill="#9999ff"
             StrokeThickness="1"/>
</Canvas>

Then you can add a Border around its Bounding Box with the following code

private void AddBorderForFrameworkElement(FrameworkElement element)
{
    Rect bounds = element.TransformToVisual(canvas).TransformBounds(new Rect(element.RenderSize));
    Border boundingBox = new Border { BorderBrush = Brushes.Red, BorderThickness = new Thickness(1) };
    Canvas.SetLeft(boundingBox, bounds.Left);
    Canvas.SetTop(boundingBox, bounds.Top);
    boundingBox.Width = bounds.Width;
    boundingBox.Height = bounds.Height;
    canvas.Children.Add(boundingBox);            
}

However, you may not always get the desired results using this since the Bounding Box will not always be the Bounds for what is actually drawn. If you instead define your Polygon like below where you start to draw where x=100 then the Bounding Box will be much larger then what is drawn

<Polygon Name="polygon" Canvas.Left="140" Canvas.Top="12"
         Points="100,75 200,0 200,150 100,75"
         Stroke="Purple"
         Fill="#9999ff"
         StrokeThickness="1"/>

enter image description here
Bounding Box comparison