Question

Does anyone know an efficient way of getting the length of a WPF geometry in Pixels? I know that geometries in WPF are vector based and therefore do not really have a pixellength. But it has to be possible to get the length based on the visible drawn image. I mean if I draw some geometries in a 1024x800 pixel image it has to be possible to get the length of those.

Am I wrong here or is there any possible and efficient way of getting these informations?

Thank you in advance!

Michael

Was it helpful?

Solution

I have come up with a solution, but don't know if this would be the most efficent (fastest) way of doing so. Please leave your comments on this - thank you all!

    public static double GetLength(this Geometry geo)
    {
        PathGeometry path = geo.GetFlattenedPathGeometry();

        double length = 0.0;

        foreach (PathFigure pf in path.Figures)
        {
            Point start = pf.StartPoint;

            foreach (PolyLineSegment seg in pf.Segments)
            {
                foreach (Point point in seg.Points)
                {
                    length += Distance(start, point);
                    start = point;
                }
            }
        }

        return length;
    }

    private static double Distance(Point p1, Point p2)
    {
        return Math.Sqrt(Math.Pow(p1.X - p2.X,2) + Math.Pow(p1.Y - p2.Y,2));
    }

OTHER TIPS

var geometry = new EllipseGeometry(new Point(50,50), 45, 20);
var length = geometry.Bounds.Width; //or geometry.Bounds.Height

Note: EllipseGeometry is just an example. All classes that derive from Geometry have a Bounds property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top