Hossein Narimani Rad
Hossein Narimani Rad

Reputation: 32481

WPF; Convert between Path Markup Syntax and Geometry in code behind

I'm looking for conversion between path markup and Geometry. I found a good post showing how to get the Geometry from path markup:

Path Markup Syntax to Geometry

string pathMarkup = "M 100,200 C 100,25 400,350 400,175 H 280";
Geometry myGeometry = Geometry.Parse(pathMarkup);

Geometry to Path Markup Syntax

Now what if I want to get the path markup from the existing geometry?

Geometry myGeometry = //some geometry
string pathMarkup = ??

Any idea how to convert a geometry to its equivalent path markup?

Upvotes: 3

Views: 2000

Answers (2)

Ben
Ben

Reputation: 3391

Expanding on Hamlet Hakobyan's answer, unfortunately the ToString() approach only works with Path Geometries. So in order to apply this generically to all Geometry types:

Geometry myGeometry = PathGeometry.Parse("M 8, 0 L 2,25 16,25 Z");
string pathString = myGeometry.ToString(); // Works only for PathGeometry

GeometryGroup geomGroup = new GeometryGroup();
geomGroup.Children.Add(myGeometry);
geomGroup.Transform = myTransform;

string pathString = PathGeometry.CreateFromGeometry(geomGroup).ToString();

Upvotes: 3

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

What about

Geometry myGeometry = //some geometry
string pathMarkup = myGeometry.ToString();

Upvotes: 4

Related Questions