将几何/路径转换为迷你语言字符串?

发布于 2024-10-20 18:58:43 字数 72 浏览 2 评论 0原文

追踪如何以编程方式将路径字符串转换为 WPF 中的路径对象并不难,但是是否有内置函数可以将几何图形或路径转换回迷你语言中的字符串?

It's not too hard to track down how to programmatically convert path strings into path objects in WPF, but is there a built-in function to convert a geometry or path back to a string in the mini-language?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2024-10-27 18:58:43

编辑:刚才看到这个,我认为应该有一个名为GeometryConverter 应该能够做到这一点,而且确实有。只需创建其中之一并使用 ConvertToString 位于要转换的几何图形上。


您可以使用 XamlWriter 类来输出对象作为 XAML,几何图形将自动简化为迷你语言。

例如,如果这是您的输入:

<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 从中获取数据。

Edit: Looking at this just now i thought that there should be a class called GeometryConverter which should be able to do this, and indeed there is. Just create one of those and use ConvertToString on the geometry you want to convert.


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

e.g. if this is your input:

<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>

...and you serialize it...

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);

...you get the following:

<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>

All the PathGeometry is now in mini-language. If you want to use this right away in your application i suppose you could write it to a MemoryStream and get the data from it by creating a XmlDocument from it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文