如何:使用 StreamGeometry 创建形状

在创建几何形状时,StreamGeometry 是对 PathGeometry 的轻量替代。 当需要描述复杂几何图形,但又不希望产生支持数据绑定、动画或修改的开销时,请使用 StreamGeometry。 例如,由于 StreamGeometry 类的高效性,该类是描述装饰器的不错选择。

创建三角形 StreamGeometry

以下示例使用特性语法在 XAML 中创建三角形 StreamGeometry

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <StackPanel>
  
    <Path Data="F0 M10,100 L100,100 100,50Z" 
      StrokeThickness="1" Stroke="Black"/>

  </StackPanel>
</Page>

有关 StreamGeometry 特性语法的详细信息,请参阅路径标记语法页面。

使用 StreamGeometry 定义三角形

下一个示例使用 StreamGeometry 在代码中定义三角形。 首先,该示例创建 StreamGeometry,然后获取 StreamGeometryContext 并使用它来描述三角形。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SDKSample
{
    // Use StreamGeometry with StreamGeometryContext to define a triangle shape.
    public partial class StreamGeometryTriangleExample : Page
    {
        public StreamGeometryTriangleExample()
        {
            // Create a path to draw a geometry with.
            Path myPath = new Path();
            myPath.Stroke = Brushes.Black;
            myPath.StrokeThickness = 1;

            // Create a StreamGeometry to use to specify myPath.
            StreamGeometry geometry = new StreamGeometry();
            geometry.FillRule = FillRule.EvenOdd;

            // Open a StreamGeometryContext that can be used to describe this StreamGeometry
            // object's contents.
            using (StreamGeometryContext ctx = geometry.Open())
            {

                // Begin the triangle at the point specified. Notice that the shape is set to
                // be closed so only two lines need to be specified below to make the triangle.
                ctx.BeginFigure(new Point(10, 100), true /* is filled */, true /* is closed */);

                // Draw a line to the next specified point.
                ctx.LineTo(new Point(100, 100), true /* is stroked */, false /* is smooth join */);

                // Draw another line to the next specified point.
                ctx.LineTo(new Point(100, 50), true /* is stroked */, false /* is smooth join */);
            }

            // Freeze the geometry (make it unmodifiable)
            // for additional performance benefits.
            geometry.Freeze();

            // Specify the shape (triangle) of the Path using the StreamGeometry.
            myPath.Data = geometry;

            // Add path shape to the UI.
            StackPanel mainPanel = new StackPanel();
            mainPanel.Children.Add(myPath);
            this.Content = mainPanel;
        }
    }
}

Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes

Namespace SDKSample
    ' Use StreamGeometry with StreamGeometryContext to define a triangle shape.
    Partial Public Class StreamGeometryTriangleExample
        Inherits Page
        Public Sub New()
            ' Create a path to draw a geometry with.
            Dim myPath As New Path()
            myPath.Stroke = Brushes.Black
            myPath.StrokeThickness = 1

            ' Create a StreamGeometry to use to specify myPath.
            Dim geometry As New StreamGeometry()
            geometry.FillRule = FillRule.EvenOdd

            ' Open a StreamGeometryContext that can be used to describe this StreamGeometry 
            ' object's contents.
            Using ctx As StreamGeometryContext = geometry.Open()

                ' Begin the triangle at the point specified. Notice that the shape is set to 
                ' be closed so only two lines need to be specified below to make the triangle.
                ctx.BeginFigure(New Point(10, 100), True, True) ' is closed  -  is filled 

                ' Draw a line to the next specified point.
                ctx.LineTo(New Point(100, 100), True, False) ' is smooth join  -  is stroked 

                ' Draw another line to the next specified point.
                ctx.LineTo(New Point(100, 50), True, False) ' is smooth join  -  is stroked 
            End Using

            ' Freeze the geometry (make it unmodifiable)
            ' for additional performance benefits.
            geometry.Freeze()

            ' Specify the shape (triangle) of the Path using the StreamGeometry.
            myPath.Data = geometry

            ' Add path shape to the UI.
            Dim mainPanel As New StackPanel()
            mainPanel.Children.Add(myPath)
            Me.Content = mainPanel
        End Sub
    End Class
End Namespace

在代码中创建几何形状

下一个示例创建一个方法,该方法使用 StreamGeometryStreamGeometryContext 根据指定参数定义几何形状。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SDKSample
{
    public partial class StreamGeometryExample : Page
    {
        public StreamGeometryExample()
        {
            // Create a path to draw a geometry with.
            Path myPath = new Path();
            myPath.Stroke = Brushes.Black;
            myPath.StrokeThickness = 1;

            // Create a StreamGeometry to use to specify myPath.
            StreamGeometry theGeometry = BuildRegularPolygon(new Point(200, 200), 200, 8, 0);
            theGeometry.FillRule = FillRule.EvenOdd;

            // Freeze the geometry (make it unmodifiable)
            // for additional performance benefits.
            theGeometry.Freeze();

            // Use the StreamGeometry returned by the BuildRegularPolygon to
            // specify the shape of the path.
            myPath.Data = theGeometry;

            // Add path shape to the UI.
            StackPanel mainPanel = new StackPanel();
            mainPanel.Children.Add(myPath);
            this.Content = mainPanel;
        }

        StreamGeometry BuildRegularPolygon(Point c, double r, int numSides, double offsetDegree)
        {
            // c is the center, r is the radius,
            // numSides the number of sides, offsetDegree the offset in Degrees.
            // Do not add the last point.

            StreamGeometry geometry = new StreamGeometry();

            using (StreamGeometryContext ctx = geometry.Open())
            {
                double step = 2 * Math.PI / Math.Max(numSides, 3);
                Point cur = c;

                double a = Math.PI * offsetDegree / 180.0;
                for (int i = 0; i < numSides; i++, a += step)
                {
                    cur.X = c.X + r * Math.Cos(a);
                    cur.Y = c.Y + r * Math.Sin(a);
                    if (i == 0)
                        ctx.BeginFigure(cur, true /* is filled */, true /* is closed */);
                    else
                        ctx.LineTo(cur, true /* is stroked */, false /* is smooth join */);
                }
            }

            return geometry;
        }
    }
}

Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes

Namespace SDKSample
    Partial Public Class StreamGeometryExample
        Inherits Page
        Public Sub New()
            ' Create a path to draw a geometry with.
            Dim myPath As New Path()
            myPath.Stroke = Brushes.Black
            myPath.StrokeThickness = 1

            ' Create a StreamGeometry to use to specify myPath.
            Dim theGeometry As StreamGeometry = BuildRegularPolygon(New Point(200, 200), 200, 8, 0)
            theGeometry.FillRule = FillRule.EvenOdd

            ' Freeze the geometry (make it unmodifiable)
            ' for additional performance benefits.
            theGeometry.Freeze()

            ' Use the StreamGeometry returned by the BuildRegularPolygon to 
            ' specify the shape of the path.
            myPath.Data = theGeometry

            ' Add path shape to the UI.
            Dim mainPanel As New StackPanel()
            mainPanel.Children.Add(myPath)
            Me.Content = mainPanel
        End Sub

        Private Function BuildRegularPolygon(ByVal c As Point, ByVal r As Double, ByVal numSides As Integer, ByVal offsetDegree As Double) As StreamGeometry
            ' c is the center, r is the radius,
            ' numSides the number of sides, offsetDegree the offset in Degrees.
            ' Do not add the last point.

            Dim geometry As New StreamGeometry()

            Using ctx As StreamGeometryContext = geometry.Open()

                Dim [step] As Double = 2 * Math.PI / Math.Max(numSides, 3)
                Dim cur As Point = c

                Dim a As Double = Math.PI * offsetDegree / 180.0
                Dim i As Integer = 0

                For i = 0 To numSides - 1
                    cur.X = c.X + r * Math.Cos(a)
                    cur.Y = c.Y + r * Math.Sin(a)
                    If i = 0 Then
                        ctx.BeginFigure(cur, True, True) ' is closed  -  is filled 
                    Else
                        ctx.LineTo(cur, True, False) ' is smooth join  -  is stroked 
                    End If
                    a += [step]
                Next

            End Using

            Return geometry
        End Function
    End Class
End Namespace

另请参阅