简体   繁体   中英

Get pointcollection from a path in wpf

I have a path object drawn using PolyBezierSegment . It is added on a Canvas . I have to fetch the object from the Canvas.Children list in a different code block and then need to retrieve the PointCollection of the fetched Path.

How can I get the PointCollection?

After doing some more research and thinking I found the answer.

public static PointCollection getPointsFromPath(Path path)
    {


        PointCollection pathPointsCollection = new PointCollection();

        PathFigure pathFigure = null;
        PolyBezierSegment polyBezierSegment = null;
        PathGeometry pathGeometry = (PathGeometry)path.Data;
        PathFigureCollection pathFigureCollection = pathGeometry.Figures;
        if (pathFigureCollection.Count > 0)
        {
            try
            {
                pathFigure = pathFigureCollection.ElementAt(0);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception found at getting path figure=" + e);
            }

            if (pathFigure != null)
            {
                PathSegmentCollection pathSegmentCollection = pathFigure.Segments;

                try
                {
                    polyBezierSegment = (PolyBezierSegment)pathSegmentCollection[0];
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception found at getting polyBezierSegment =" + e);
                }
                if (polyBezierSegment != null)
                {
                    pathPointsCollection = polyBezierSegment.Points;
                }


            }

        }

        //Console.WriteLine("pathPointsCollection.Count=" + pathPointsCollection.Count + " values=" + pathPointsCollection.ToString());

        return pathPointsCollection;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM