StreamGeometry から Point オブジェクト は 取得出来ない

Uncategorized
419 words

タイトルの通りです。私自身知らなくて、四苦八苦していました。

一度描画した線から Point オブジェクト を取得したい場合は「PathGeometry」を使います。

マウスとかで線をグリグリしたいなら「PathGeometry」、装飾品のように変わらない線を描きたいなら「StreamGeometry」を使い分けた方がよいです。

「StreamGeometry」は、データバインディングやアニメーション、線の変更ができない替わりに描画の負荷が少ないらしい。

XAML で描いた Path は StreamGeometry になる

試しに、XAML で Path を描いて クイックウォッチでデータ構造を見てみます。

1
2
3
4
5
6
7
8
9
<Path
x:Name="path"
Data="M 0,0 C 0,50 100,50 100,100"
Width="100"
Height="100"
Stroke="#FF076901"
StrokeThickness="3"
Stretch="Fill"
/>

Path が StreamGeometry 型 で Point オブジェクトが存在しません。

コードビハインドで StreamGeometry の線を引く

次に、XAML で描いた線と同じ線を コードビハインド で描いてみます。

1
2
3
4
5
6
7
8
9
StreamGeometry geometry = new StreamGeometry();
geometry.FillRule = FillRule.EvenOdd;
using (StreamGeometryContext ctx = geometry.Open())
{
ctx.BeginFigure(new Point(0, 0), true, false);
ctx.BezierTo(new Point(0, 50), new Point(100, 50), new Point(100, 100), true, false);
}
geometry.Freeze();
path.Data = geometry;

XAML で描いたときと同じデータ構造になっていることが分かります。

コードビハインドで PathGeometry の線を引く

XAML で PathGeometry の線 が描けないので、コードビハインド で PathGeometry の線を描きます。

1
2
3
4
5
6
7
PathGeometry geometry = new PathGeometry();
PathFigure pathFigure = new PathFigure();
pathFigure.StartPoint = new Point(0, 0);
pathFigure.Segments.Add(
new BezierSegment(new Point(0, 50), new Point(100, 50), new Point(100, 100), true));
geometry.Figures.Add(pathFigure);
path.Data = geometry;

クイックウォッチで Path のデータ構造を見ると PathGeometry 型 なのが分かります。

PathGeometry の Point を取得するには?

PathGeometry の Point を取得するには?

1
var point3 = ((BezierSegment)((PathGeometry)path.Data).Figures[0].Segments[0]).Point3;

これで、一度描画した線でもグリグリ動かす事ができます。