将几何转换为wpf中的路径(使用混合?) [英] Convert a geometry to a path in wpf (with blend ?)

查看:335
本文介绍了将几何转换为wpf中的路径(使用混合?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单。

我该如何转换这段代码:

 <路径> 
< Path.Data>
< EllipseGeometry Center =5,4RadiusX =4RadiusY =4/>
< /Path.Data>
< / Path>

类似于

 <路径数据=M 0 5 L 3 10 10 0/> 

(请注意,第二个选项会产生一个复选标记,而不是椭圆形,仅用于说明目的,我的目标就是这样:找到序列给出的椭圆)

编辑:我还阅读了关于xaml中Bezier曲线的文档,并且完全意识到我可以简单地通过计算贝塞尔曲线的精确点来得到一个椭圆来产生正确的代码,但是我不希望自己去做这个计算的麻烦,所以我想知道是否有一个简单的方法来做到这一点(在混合也许)

解决方案

这显示了如何从代码执行此操作。我不知道这是否能解决你的问题 - 你说混合?在标题中,我不知道在Blend中做到这一点的方法。但我希望这可能会有所帮助。

第一步是将 EllipseGeometry 转换为 PathGeometry

  var geom = new EllipseGeometry(new Point(5,4),4 ,4); 
var pathGeometry = PathGeometry.CreateFromGeometry(geom);

一旦您获得了 PathGeometry ,您可以简单地调用 ToString ,它会给你字符串表示形式:

  string pathText = pathGeometry.ToString(); 

在我的系统中,这会生成以下相当详细的文本:

M9,4C9,6.20913899932317 7.20913899932317,8 5,8 2.79086100067683,8 1,6.20913899932317 1,4 1,1.79086100067683 2.79086100067683,0 5,0 7.20913899932317,0 9,1.79086100067683 9,4z



现在,如果您希望最终得到您将该字符串输入到Xaml中的内容,那么您需要再多一步,因为您提供的表单的Xaml :

 < Path Data =M 0 5 L 3 10 10 0/> 

不会产生 PathGeometry 。它产生一个 StreamGeometry ,这是一个稍微更高效但固定的路径表示。 (主要区别在于,您无法获取代表 StreamGeometry 中的各种图形和区域的单个对象,而您可以使用 PathGeometry 。 StreamGeometry 会更便宜。)



您可以获得 StreamGeometry 来自路径文本:

  var streamGeometry = StreamGeometry.Parse(pathText ); 

然后如果你想在 Path中找到只是建立它:

  var p = new Path {Data = streamGeometry}; 

根据您确切的目标是什么,可能会有更有效的方法来做到这一点。 ( StreamGeometry 是一种更高效的表示方式,因为它最终创建的对象数量少得多,但是我的代码获取到的表示路径效率不高,因此您可能会更好只需停止第一个代码片段生成的 PathGeometry ;或者,如果您的目标仅仅是获取路径文本,那么 PathGeometry 也足以满足您的需求。)


my question is simple.

how can I convert this code:

<Path>
    <Path.Data>
        <EllipseGeometry Center="5,4" RadiusX="4" RadiusY="4"/>
    </Path.Data>
</Path>

into something like

<Path Data="M 0 5 L 3 10 10 0"/>

(please note that the second one produces a checkmark, not an ellipse. It was just for illustration purposes, and my goal is just that: find what sequence gives an ellipse)

edit: I also read the doc about Bezier curves in xaml and am fully aware that I could simply produce the right code by calculating the exact points of the Bezier curve to get an ellipse, but I do not wish to go to the hassle of doing this calculation myself, so I was wondering if there is an easy way to do this (in Blend maybe)

解决方案

This shows how to do it from code. I'm not sure if that'll solve your problem or not - you say "with blend?" in the title, and I'm not aware of a way to do this in Blend. But I hope this may help.

The first step would be to convert the EllipseGeometry into a PathGeometry:

var geom = new EllipseGeometry(new Point(5, 4), 4, 4);
var pathGeometry = PathGeometry.CreateFromGeometry(geom);

Once you've got a PathGeometry you can simply call ToString and it'll give you the string representation:

string pathText = pathGeometry.ToString();

On my system, this produces the following rather verbose text:

"M9,4C9,6.20913899932317 7.20913899932317,8 5,8 2.79086100067683,8 1,6.20913899932317 1,4 1,1.79086100067683 2.79086100067683,0 5,0 7.20913899932317,0 9,1.79086100067683 9,4z"

Now if you want to end up with exactly what you would have got if you'd fed that string into Xaml, you need one more step, because Xaml of the form you've supplied:

<Path Data="M 0 5 L 3 10 10 0"/>

doesn't produce a PathGeometry. It produces a StreamGeometry, which is a slightly more efficient, but fixed representation of the path. (The main difference being that you can't get hold of individual objects representing the various figures and segements in a StreamGeometry, whereas you can with a PathGeometry. StreamGeometry is cheaper, as a result.)

You can get a StreamGeometry from the path text:

var streamGeometry = StreamGeometry.Parse(pathText);

And then if you want that in a Path just build it:

var p = new Path { Data = streamGeometry };

Depending on what your exact goal is, there may be more efficient ways to do this. (StreamGeometry is a more efficient representation because it ends up creating far fewer objects. But the route by which my code gets to that representation is not very efficient, so you might be better off just stopping with the PathGeometry that the very first code snippet produces. Alternatively, if your goal is really just to get the path text, then the PathGeometry is also sufficient for your needs.)

这篇关于将几何转换为wpf中的路径(使用混合?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆