Rabi
Rabi

Reputation: 135

How to store PathGeometry?

I have several PathGeometry-s which are showing line between places. I want to save these lines. I couldn't serialize it to XML and don't know how to store these lines. Is there any chance to do it ?

[EDIT] I have PathGeometry-s at code behind, not in XAML. When i try to use XMLWriter class to store these it gives exception "... classes couldn't be restored because they dont be known as known types "

Upvotes: 0

Views: 1670

Answers (3)

Dominik
Dominik

Reputation: 3372

In case you have the PathGeometry already instantiated as an object you can try to use the XAMLWriter to serialize it MSDN

e.g.

        var pathgeo = new PathGeometry();
        pathgeo.AddGeometry(new PathGeometry(new PathFigureCollection() { new PathFigure(new Point(10, 50), new List<PathSegment>() { new LineSegment(new Point(200, 70), true) }, false) }));

        // Save
        var s = XamlWriter.Save(pathgeo);

        byte[] byteArray = Encoding.ASCII.GetBytes(s);
        var stream = new MemoryStream(byteArray);

        // Load
        var ob = XamlReader.Load(stream);

        // test beeing a System.Windows.Shapes.Path
        test.Data = ob as PathGeometry;

Upvotes: 4

Javidan
Javidan

Reputation: 594

You could serialize PathGeomety-s string representations in collection and when you want to restore it , use this great solution to restore

http://stringtopathgeometry.codeplex.com

There is method StringToPathConvertor.Convert(string path) which returns PathGeometry.

Upvotes: 1

AquilaRapax
AquilaRapax

Reputation: 1099

Why couldn't you store it to xml? What about <Line x1="0" y1="0" x2="100" y2="100"/>? Maybe you can give a little example?

Upvotes: 0

Related Questions