How do I convert PathGeometry back to String in .net maui?

CGP 0 Reputation points

I know I am able to load string and convert to Geomerty using the following

(Geometry)new PathGeometryConverter().ConvertFromInvariantString.

basically, I would like to get the string representation back from Geomerty object

found the following links but its not applicable for .net maui? any advise...

A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,971 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,386 Reputation points Microsoft Vendor


    found the following links but its not applicable for .net maui?

    Yes, this link used Geomerty from System.Windows.Media namespace. But you used Geomerty from Microsoft.Maui.Controls.Shapes they are different.

    I cannot find Api to convert Geometry to the string directly, if you need it, we can convert it by ourselves.

    Firstly, we cannot get point data from the Geometry, ConvertFromInvariantString retrun the PathGeometry, we can use it to get the startPoint, for the start point, we need add "M" at the begin and add L in the end of the start point.

    To get following points, we need to get PathSegmentCollection by pathFigure.Segments, and traverse the PathSegmentCollection to get all the remaining points, by the way, do not add Z at the end of the string.

    string OrString = "M13.908992,16.207977 L32.000049,16.207977 32.000049,31.999985 13.908992,30.109983Z";
      PathGeometry pathData = (PathGeometry)new PathGeometryConverter().ConvertFromInvariantString(OrString);
      PathFigure pathFigure= pathData.Figures.FirstOrDefault();
     StringBuilder GeometryToString = new StringBuilder();
      Point startPoint=  pathFigure.StartPoint;          
      GeometryToString.Append("M"+startPoint.X+","+ startPoint.Y+" L");
      PathSegmentCollection segments= pathFigure.Segments;
      for (int i = 0; i < segments.Count; i++)
          LineSegment lineSegment= segments[i] as LineSegment;
         if (i.Equals(segments.Count - 1))
              GeometryToString.Append(lineSegment.Point.X + "," + lineSegment.Point.Y + "Z");
              GeometryToString.Append(lineSegment.Point.X + "," + lineSegment.Point.Y + " ");
    var endGeometryString =  GeometryToString.ToString();

    Best Regards,

    Leon Lu

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments