Search code examples
c#wpfpathgeometry

Getting actual path of PathGeometry


Is there a way to get the actual path of a PathGeometry in WPF? I've looked at RenderedGeometry, but it doesn't appear to provide anything other than what I put in.

For example, here's my XAML:

<Path x:Name="right" Canvas.Left="10" Canvas.Top="10" StrokeThickness="3" 
      Stroke="Black" StrokeEndLineCap="Round" StrokeStartLineCap="Round" 
      StrokeLineJoin="Miter" Data="M0,9L4.5,0L9,9 "/>`

This produces:
enter image description here

Does WPF provide any function natively or is there way to get the traced outline of this shape in path data?

I've also tried Petzold's attempt at something similar to this here, but it simply doesn't work.


Solution

  • Use GetWidenedPathGeometry with a Pen that applies all relevant stroke-related properties from the source Path.

    var pen = new Pen
    {
        Thickness = right.StrokeThickness,
        StartLineCap = right.StrokeStartLineCap,
        EndLineCap = right.StrokeEndLineCap,
        LineJoin = right.StrokeLineJoin,
        MiterLimit = right.StrokeMiterLimit
    };
    
    var geometry = right.Data.GetWidenedPathGeometry(pen);