从wpf中的路径获取pointcollection

时间:2015-03-19 08:54:32

标签: c# .net wpf

我有一个使用PolyBezierSegment绘制的路径对象。它被添加到Canvas。我必须从另一个代码块中的Canvas.Children列表中获取对象,然后需要检索所获取路径的PointCollection

我如何获得PointCollection?

1 个答案:

答案 0 :(得分:0)

经过更多的研究和思考后,我找到了答案。

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;
    }