SIGN UP MEMBER LOGIN:    
ARTICLE

Polyline in WPF

Posted by Mahesh Chand Articles | WPF February 04, 2010
A polyline is a collection of connected straight lines. This article demonstrates how to create a polyline in WPF and XAML.
Reader Level:

A polyline is a collection of connected straight lines. The Polyline object represents a polyline shape and draws a polyline with the given points. The Points property represents the points in a polyline. The Stroke property sets the color and StrokeThickness represents the width of the line of a polyline.

Creating a Polyline

The Polyline element in XAML creates a polyline shape. The following code snippet creates a polyline by setting its Points property. The code also sets the black stroke of width 4. 

<Polyline

    Points="10,100 100,200 200,30 250,200 200,150"

    Stroke="Black"

    StrokeThickness="4" />

 

The output looks like Figure 12.

PolylineImg.gif 

Figure 12. A Polyline

The CreateAPolyline method listed in Listing 11 draws same rectangle in Figure 12 dynamically.

private void CreateAPolyline()

{

    // Create a blue and a black Brush

    SolidColorBrush yellowBrush = new SolidColorBrush();

    yellowBrush.Color = Colors.Yellow;

    SolidColorBrush blackBrush = new SolidColorBrush();

    blackBrush.Color = Colors.Black;

 

    // Create a polyline

    Polyline yellowPolyline = new Polyline();

    yellowPolyline.Stroke = blackBrush;

    yellowPolyline.StrokeThickness = 4;

 

    // Create a collection of points for a polyline

    System.Windows.Point Point1 = new System.Windows.Point(10, 100);

    System.Windows.Point Point2 = new System.Windows.Point(100, 200);

    System.Windows.Point Point3 = new System.Windows.Point(200, 30);

    System.Windows.Point Point4 = new System.Windows.Point(250, 200);

    System.Windows.Point Point5 = new System.Windows.Point(200, 150);

    PointCollection polygonPoints = new PointCollection();

    polygonPoints.Add(Point1);

    polygonPoints.Add(Point2);

    polygonPoints.Add(Point3);

    polygonPoints.Add(Point4);

    polygonPoints.Add(Point5);

 

    // Set Polyline.Points properties

    yellowPolyline.Points = polygonPoints;

 

    // Add polyline to the page

    LayoutRoot.Children.Add(yellowPolyline);

}

Listing 11

erver'>
share this article :
post comment
 

Is there any memory or performance limitations using polylines or vectors?

Posted by Ray Akkanson Mar 22, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor