Share via


How to: Use a Pen to Draw Lines

To draw lines, you need a Graphics object and a Pen object. The Graphics object provides the DrawLine method, and the Pen object stores features of the line, such as color and width.

Example

The following example draws a line from (20, 10) to (300, 100). The first statement uses the Pen class constructor to create a black pen. The one argument passed to the Pen constructor is a Color object created with the FromArgb method. The values used to create the Color object — (255, 0, 0, 0) — correspond to the alpha, red, green, and blue components of the color. These values define an opaque black pen.

Dim pen As New Pen(Color.FromArgb(255, 0, 0, 0))
e.Graphics.DrawLine(pen, 20, 10, 300, 100)
Pen pen = new Pen(Color.FromArgb(255, 0, 0, 0));
e.Graphics.DrawLine(pen, 20, 10, 300, 100);

Compiling the Code

The preceding example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler.

See Also

Concepts

Pens, Lines, and Rectangles in GDI+

Reference

Pen

Other Resources

Using a Pen to Draw Lines and Shapes