Getting pixel positions for all WPF path points

Let's say you have a Bezier spline drawn on canvas like this:

<Canvas x:Name="SomeCanvas" Width="50" Height="50" Background="Black">
    <Path x:Name="SomePath" Data="M0,0C10,10 10,50 50,10" Stroke="Yellow" StrokeThickness="1"/>
</Canvas>

How would you determine which pixels in the canvas intersect the center of the path (this means that the stroke thickness is not taken into account)?

+4
source share
1 answer

If you want to find the center point along . I think that we have to do something with the data of the path, which actually is Geometry. A Geometryhas a method called GetFlattenedGeometryPaththat returns a PathGeometry, which has a method called GetPointAtFractionLength. So you can try something like this:

 Point centerPoint;
 Point tg;
 SomePath.Data.GetFlattenedGeometryPath()
              .GetPointAtFractionLength(0.5, out centerPoint, out tg);

, , , . . , 1000 , , :

Point p;
Point tg;
var points = new List<Point>();
for(var i = 0; i < 1000; i++){
  SomePath.Data.GetFlattenedGeometryPath()
               .GetPointAtFractionLength(i / 1000f, out p, out tg);
  points.Add(p);
}
+5

All Articles