Files
docs-desktop/dotnet-desktop-guide/framework/wpf/graphics-multimedia/how-to-create-a-linesegment-in-a-pathgeometry.md
T
Andy De George da363692ff Initial WPF content migrated (#17)
* Reset branch for WPF changes

* Convert BMP to PNG; fix link-out-of-scope err

* Add snippets for WPF... 6794 files!!!!

* Add missing snippets

* update file updated between migration

* Fix paths to include

* update breadcrumb and toc

* fix index links

* fix index links

* fix index links

* fix markdown
2020-09-04 09:46:28 -07:00

3.5 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Create a LineSegment in a PathGeometry 03/30/2017
csharp
vb
line segments [WPF], creating
graphics [WPF], line segments
0155ed47-a20d-49a7-a306-186d8e07fbc4

How to: Create a LineSegment in a PathGeometry

This example shows how to create a line segment. To create a line segment, use the xref:System.Windows.Media.PathGeometry, xref:System.Windows.Media.PathFigure, and xref:System.Windows.Media.LineSegment classes.

Example

The following examples draw a xref:System.Windows.Media.LineSegment from (10, 50) to (200, 70). The following illustration shows the resulting xref:System.Windows.Media.LineSegment; a grid background was added to show the coordinate system.

A LineSegment in a PathFigure A LineSegment drawn from (10,50) to (200,70)

[xaml]

In [!INCLUDETLA#tla_xaml], you may use attribute syntax to describe a path.

<Path Stroke="Black" StrokeThickness="1"
  Data="M 10,50 L 200,70" />

[xaml]

(Note that this attribute syntax actually creates a xref:System.Windows.Media.StreamGeometry, a lighter-weight version of a xref:System.Windows.Media.PathGeometry. For more information, see the Path Markup Syntax page.)

In [!INCLUDETLA2#tla_xaml], you may also draw a line segment by using object element syntax. The following is equivalent to the previous [!INCLUDETLA2#tla_xaml] example.

<Path Stroke="Black" StrokeThickness="1">
  <Path.Data>
    <PathGeometry>
      <PathFigure StartPoint="10,50">
        <LineSegment Point="200,70" />
      </PathFigure>
    </PathGeometry>
  </Path.Data>
</Path>
PathFigure myPathFigure = new PathFigure();
myPathFigure.StartPoint = new Point(10, 50);

LineSegment myLineSegment = new LineSegment();
myLineSegment.Point = new Point(200, 70);

PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
myPathSegmentCollection.Add(myLineSegment);

myPathFigure.Segments = myPathSegmentCollection;

PathFigureCollection myPathFigureCollection = new PathFigureCollection();
myPathFigureCollection.Add(myPathFigure);

PathGeometry myPathGeometry = new PathGeometry();
myPathGeometry.Figures = myPathFigureCollection;

Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
myPath.Data = myPathGeometry;
Dim myPathFigure As New PathFigure()
myPathFigure.StartPoint = New Point(10, 50)

Dim myLineSegment As New LineSegment()
myLineSegment.Point = New Point(200, 70)

Dim myPathSegmentCollection As New PathSegmentCollection()
myPathSegmentCollection.Add(myLineSegment)

myPathFigure.Segments = myPathSegmentCollection

Dim myPathFigureCollection As New PathFigureCollection()
myPathFigureCollection.Add(myPathFigure)

Dim myPathGeometry As New PathGeometry()
myPathGeometry.Figures = myPathFigureCollection

Dim myPath As New Path()
myPath.Stroke = Brushes.Black
myPath.StrokeThickness = 1
myPath.Data = myPathGeometry

This example is part of larger sample; for the complete sample, see the Geometries Sample.

See also