Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/pens-lines-and-rectangles-in-gdi.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

6.1 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
Pens, Lines, and Rectangles in GDI+ 03/30/2017
csharp
vb
lines
GDI+, lines
drawing [Windows Forms], rectangles
rectangles
drawing [Windows Forms], lines
GDI+, pens
examples [Windows Forms], drawing lines and shapes
examples [Windows Forms], pens
GDI+, rectangles
examples [Windows Forms], GDI+
lines [Windows Forms], dashed
30b25aae-e3eb-4479-bdb8-187cf651fc84

Pens, Lines, and Rectangles in GDI+

To draw lines with GDI+ you need to create a xref:System.Drawing.Graphics object and a xref:System.Drawing.Pen object. The xref:System.Drawing.Graphics object provides the methods that actually do the drawing, and the xref:System.Drawing.Pen object stores attributes, such as line color, width, and style.

Drawing a Line

To draw a line, call the xref:System.Drawing.Graphics.DrawLine%2A method of the xref:System.Drawing.Graphics object. The xref:System.Drawing.Pen object is passed as one of the arguments to the xref:System.Drawing.Graphics.DrawLine%2A method. The following example draws a line from the point (4, 2) to the point (12, 6):

[!code-csharpLinesCurvesAndShapes#41] [!code-vbLinesCurvesAndShapes#41]

xref:System.Drawing.Graphics.DrawLine%2A is an overloaded method of the xref:System.Drawing.Graphics class, so there are several ways you can supply it with arguments. For example, you can construct two xref:System.Drawing.Point objects and pass the xref:System.Drawing.Point objects as arguments to the xref:System.Drawing.Graphics.DrawLine%2A method:

[!code-csharpLinesCurvesAndShapes#42] [!code-vbLinesCurvesAndShapes#42]

Constructing a Pen

You can specify certain attributes when you construct a xref:System.Drawing.Pen object. For example, one Pen constructor allows you to specify color and width. The following example draws a blue line of width 2 from (0, 0) to (60, 30):

[!code-csharpLinesCurvesAndShapes#43] [!code-vbLinesCurvesAndShapes#43]

Dashed Lines and Line Caps

The xref:System.Drawing.Pen object also exposes properties, such as xref:System.Drawing.Pen.DashStyle%2A, that you can use to specify features of the line. The following example draws a dashed line from (100, 50) to (300, 80):

[!code-csharpLinesCurvesAndShapes#44] [!code-vbLinesCurvesAndShapes#44]

You can use the properties of the xref:System.Drawing.Pen object to set many more attributes of the line. The xref:System.Drawing.Pen.StartCap%2A and xref:System.Drawing.Pen.EndCap%2A properties specify the appearance of the ends of the line; the ends can be flat, square, rounded, triangular, or a custom shape. The xref:System.Drawing.Pen.LineJoin%2A property lets you specify whether connected lines are mitered (joined with sharp corners), beveled, rounded, or clipped. The following illustration shows lines with various cap and join styles.

Lines

Drawing a Rectangle

Drawing rectangles with GDI+ is similar to drawing lines. To draw a rectangle, you need a xref:System.Drawing.Graphics object and a xref:System.Drawing.Pen object. The xref:System.Drawing.Graphics object provides a xref:System.Drawing.Graphics.DrawRectangle%2A method, and the xref:System.Drawing.Pen object stores attributes, such as line width and color. The xref:System.Drawing.Pen object is passed as one of the arguments to the xref:System.Drawing.Graphics.DrawRectangle%2A method. The following example draws a rectangle with its upper-left corner at (100, 50), a width of 80, and a height of 40:

[!code-csharpLinesCurvesAndShapes#45] [!code-vbLinesCurvesAndShapes#45]

xref:System.Drawing.Graphics.DrawRectangle%2A is an overloaded method of the xref:System.Drawing.Graphics class, so there are several ways you can supply it with arguments. For example, you can construct a xref:System.Drawing.Rectangle object and pass the xref:System.Drawing.Rectangle object to the xref:System.Drawing.Graphics.DrawRectangle%2A method as an argument:

[!code-csharpLinesCurvesAndShapes#46] [!code-vbLinesCurvesAndShapes#46]

A xref:System.Drawing.Rectangle object has methods and properties for manipulating and gathering information about the rectangle. For example, the xref:System.Drawing.Rectangle.Inflate%2A and xref:System.Drawing.Rectangle.Offset%2A methods change the size and position of the rectangle. The xref:System.Drawing.Rectangle.IntersectsWith%2A method tells you whether the rectangle intersects another given rectangle, and the xref:System.Drawing.Rectangle.Contains%2A method tells you whether a given point is inside the rectangle.

See also