1

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.

Todd Main
  • 28,951
  • 11
  • 82
  • 146
  • 1
    I'm not sure what you mean by "the actual path". What you see is what you put in: those three points joined by lines, stroked with a pen 3 units thick, with rounded start and end caps. Are you trying to get the tessellated geometry that's created at the rendering layer? What you are seeing here isn't a really a 'shape'; it's a couple of stroked lines. – Mike Strobel Sep 23 '17 at 21:49
  • @MikeStrobel yes – Todd Main Sep 23 '17 at 21:51
  • 1
    Thanks Mike. Look at Petzold's example - also just lines with a `LineJoin=Round`. In `GetWidenedPathGeometry`, I can get my `LineJoin=Miter`, so it's "almost" the shape you see above, but the LineCaps of Round that don't show up. – Todd Main Sep 23 '17 at 21:58
  • 1
    Interesting, that link was broken for me 10 minutes ago. I'd actually forgotten about some of those more esoteric geometry-related APIs, and my initial conclusion may have been hasty. I will keep an eye on this question, and perhaps delve into it tomorrow if someone else doesn't drop in with an answer for you :). – Mike Strobel Sep 23 '17 at 22:04
  • Yeah, someone decided to edit my post and mess up the link at the same time. I changed it back to the correct link. – Todd Main Sep 23 '17 at 22:05

1 Answers1

1

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);
Clemens
  • 123,504
  • 12
  • 155
  • 268