Convert Geometry / Path to Minilanguage String?

It is not difficult to determine how to programmatically convert path strings to path objects in WPF, but is there a built-in function to convert the geometry or path back to a string in a mini-language?

+5
source share
1 answer

Edit: Looking at it now, I thought that there should be a class GeometryConverterthat should be able to do this, and really have one. Just create one of them and use ConvertToStringin the geometry you want to transform.


You can use the class XamlWriterto output objects as XAML, the geometry will automatically be reduced to a mini-language.

. :

<DrawingImage x:Name="segmentsDrawing">
    <DrawingImage.Drawing>
        <DrawingGroup>

            <GeometryDrawing Brush="Red">
                <GeometryDrawing.Pen>
                    <Pen Brush="Black" />
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                    <PathGeometry>
                        <PathFigure StartPoint="100,100">
                            <PathFigure.Segments>
                                <LineSegment Point="100,0"/>
                                <ArcSegment Point="186.6,150"  SweepDirection="Clockwise" Size="100,100"/>
                                <LineSegment Point="100,100"/>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathGeometry>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>

            <GeometryDrawing Brush="Blue">
                <GeometryDrawing.Pen>
                    <Pen Brush="Black"/>
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                    <PathGeometry>
                        <PathFigure StartPoint="100,100">
                            <PathFigure.Segments>
                                <LineSegment Point="186.6,150"/>
                                <ArcSegment Point="13.4,150" SweepDirection="Clockwise" Size="100,100"/>
                                <LineSegment Point="100,100"/>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathGeometry>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>

            <GeometryDrawing Brush="Green">
                <GeometryDrawing.Pen>
                    <Pen Brush="Black"/>
                </GeometryDrawing.Pen>
                <GeometryDrawing.Geometry>
                    <PathGeometry>
                        <PathFigure StartPoint="100,100">
                            <PathFigure.Segments>
                                <LineSegment Point="13.4,150"/>
                                <ArcSegment Point="100,0" SweepDirection="Clockwise" Size="100,100"/>
                                <LineSegment Point="100,100"/>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathGeometry>
                </GeometryDrawing.Geometry>
            </GeometryDrawing>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

... ...

XmlTextWriter writer = new XmlTextWriter(@"C:\Users\Public\Test.xml", new UTF8Encoding());
writer.Formatting = Formatting.Indented;
writer.Indentation = 1;
writer.IndentChar = '\t';
XamlWriter.Save(segmentsDrawing, writer);

... :

<DrawingImage xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <DrawingImage.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="#FFFF0000">
                    <GeometryDrawing.Pen>
                        <Pen Brush="#FF000000" />
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Geometry>
                        <PathGeometry Figures="M100,100L100,0A100,100,0,0,1,186.6,150L100,100" />
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="#FF0000FF">
                    <GeometryDrawing.Pen>
                        <Pen Brush="#FF000000" />
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Geometry>
                        <PathGeometry Figures="M100,100L186.6,150A100,100,0,0,1,13.4,150L100,100" />
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
                <GeometryDrawing Brush="#FF008000">
                    <GeometryDrawing.Pen>
                        <Pen Brush="#FF000000" />
                    </GeometryDrawing.Pen>
                    <GeometryDrawing.Geometry>
                        <PathGeometry Figures="M100,100L13.4,150A100,100,0,0,1,100,0L100,100" />
                    </GeometryDrawing.Geometry>
                </GeometryDrawing>
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

PathGeometry -. , , MemoryStream , XmlDocument.

+6

All Articles