如何从StreamGeometry中提取Point对象?

时间:2012-01-05 09:16:19

标签: c# .net wpf xaml graphics

我使用MSDN示例构建了一个简单的StreamGeometry:

StreamGeometry geometry = new StreamGeometry();
geometry.FillRule = FillRule.EvenOdd;
using (StreamGeometryContext ctx = geometry.Open())
{
     ctx.BeginFigure(new Point(10, 100), true /* is filled */, true /* is closed */);
     ctx.LineTo(new Point(100, 100), true /* is stroked */, false /* is smooth join */);
     ctx.LineTo(new Point(100, 50), true /* is stroked */, false /* is smooth join */);
 }
 return geometry;

如何从StreamGeometry返回Point对象?
我似乎找不到任何合适的方法。我只能看到 ToString(),它为我提供了迷你语言格式: {M10,100L100,100 100,50z}

3 个答案:

答案 0 :(得分:2)

创建后,您无法更改StreamGeometry。我很确定你无法访问你添加的积分。您可以使用PathGeometry吗?您可以通过PathGeometry.Figures属性访问和修改构建后的点。

答案 1 :(得分:1)

这是我们为类似任务所做的。请注意,您的几何图形不得包含任何内容。基本上它是Geometry上的一个小抽象层次,Stroke()返回一个带有mimilanguage和Points集合的元组。您可以通过编写一个简单的行为使Navigator可绑定,如果有兴趣请告诉我,我会调整我的答案。

Update 1 - 该函数,它接受minilang表达式并返回一个Point数组:

public static Point[] Parse(string minilanguage)
        {
            // leave just M's
            minilanguage = minilanguage.ToUpper().Replace("M", string.Empty);

            // remove spaces
            minilanguage = minilanguage
                .ToCharArray()
                .Where(t => t != ' ')
                .Select(t => t.ToString())
                .Aggregate((f, s) => f + s).ToString();


            return minilanguage
                .Split("L".ToCharArray())
                .Select(t => Point.Parse(t))
                .ToArray();
        }

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GeometryParts
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Navigator navigator = new Navigator(0, 0);

            navigator.NavigateTo(30, 30);
            navigator.NavigateTo(60, 0);

            Tuple<string, Point[]> stroke = navigator.Stroke();
            this.g.Data = Geometry.Parse(stroke.Item1);

            MessageBox.Show(stroke.Item2.Length.ToString());
        }
    }


    public class Navigator
    {
        private List<Point> points = new List<Point>();

        public Navigator(double x, double y)
        {
            this.points.Add(new Point(x, y));
        }

        public void NavigateTo(double x, double y)
        {
            this.points.Add(new Point(x, y));
        }

        public Tuple<string, Point[]> Stroke()
        {
            string path = this.points.Select(t => t.ToString()).Aggregate((f, s) => "L" + f + " L" + s);
            path = "M" + path.Substring(2, path.Length - 2);

            Tuple<string, Point[]> stroke = new Tuple<string, Point[]>(path, this.points.ToArray());
            points.Clear();

            return stroke;
        }
    }
}

标记:

<Window x:Class="GeometryParts.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Path Stroke="Gray" StrokeThickness="2" x:Name="g" />
    </Grid>
</Window>

答案 2 :(得分:1)

我为silverlight做了类似的事......

在以下链接中,您可以找到代码(也适用于WPF)以从地理路径获取点

从数据字符串创建路径几何。动态创建路径 http://bathinenivenkatesh.blogspot.com/2009/05/creating-path-geometry-from-data-string.html

从PathGeometary对象获取路径地理数据 http://bathinenivenkatesh.blogspot.com/2009/05/silverlight-get-path-geomatery-figures.html