--- title: "Pens, Lines, and Rectangles in GDI+" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "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" ms.assetid: 30b25aae-e3eb-4479-bdb8-187cf651fc84 --- # Pens, Lines, and Rectangles in GDI+ To draw lines with GDI+ you need to create a object and a object. The object provides the methods that actually do the drawing, and the object stores attributes, such as line color, width, and style. ## Drawing a Line To draw a line, call the method of the object. The object is passed as one of the arguments to the method. The following example draws a line from the point (4, 2) to the point (12, 6): [!code-csharp[LinesCurvesAndShapes#41](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#41)] [!code-vb[LinesCurvesAndShapes#41](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#41)] is an overloaded method of the class, so there are several ways you can supply it with arguments. For example, you can construct two objects and pass the objects as arguments to the method: [!code-csharp[LinesCurvesAndShapes#42](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#42)] [!code-vb[LinesCurvesAndShapes#42](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#42)] ## Constructing a Pen You can specify certain attributes when you construct a 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-csharp[LinesCurvesAndShapes#43](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#43)] [!code-vb[LinesCurvesAndShapes#43](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#43)] ## Dashed Lines and Line Caps The object also exposes properties, such as , that you can use to specify features of the line. The following example draws a dashed line from (100, 50) to (300, 80): [!code-csharp[LinesCurvesAndShapes#44](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#44)] [!code-vb[LinesCurvesAndShapes#44](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#44)] You can use the properties of the object to set many more attributes of the line. The and properties specify the appearance of the ends of the line; the ends can be flat, square, rounded, triangular, or a custom shape. The 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](./media/aboutgdip02-art04.gif "Aboutgdip02_art04") ## Drawing a Rectangle Drawing rectangles with GDI+ is similar to drawing lines. To draw a rectangle, you need a object and a object. The object provides a method, and the object stores attributes, such as line width and color. The object is passed as one of the arguments to the 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-csharp[LinesCurvesAndShapes#45](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#45)] [!code-vb[LinesCurvesAndShapes#45](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#45)] is an overloaded method of the class, so there are several ways you can supply it with arguments. For example, you can construct a object and pass the object to the method as an argument: [!code-csharp[LinesCurvesAndShapes#46](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#46)] [!code-vb[LinesCurvesAndShapes#46](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#46)] A object has methods and properties for manipulating and gathering information about the rectangle. For example, the and methods change the size and position of the rectangle. The method tells you whether the rectangle intersects another given rectangle, and the method tells you whether a given point is inside the rectangle. ## See also - - - - [How to: Create a Pen](how-to-create-a-pen.md) - [How to: Draw a Line on a Windows Form](how-to-draw-a-line-on-a-windows-form.md) - [How to: Draw an Outlined Shape](how-to-draw-an-outlined-shape.md)