Using Pens in GDI+

This article has been excerpted from book "Graphics Programming with GDI+".

Pens are another key object in GDI+. As mentioned earlier pens are used to draw lines and curves and the outlines of graphics shapes. A pen draws lines and curves with a specified width and style. The Pen object provided members to set the width and style of a pen. Pens can have various kinds of dashed lines and line fill styles. Actually, the process of drawing a line creates a region in the shape of a widened line, and that region is filled with a brush. The dashed lines of pens are represented by dash styles. The fill styles of lines can be solids or textures depending on the brush used to create a Pen object.

In this section we will discuss how to create and use pens in GDI+; the Pen and Pens classes; and how to create dash styles, cap styles, and line styles for pens.

Creating Pens

The Pen class represents a pen in GDI+. Using the Pen class constructor, an application can create a Pen object from a Brush or Color object with a specified width for the pen.

Listing 4.15 create pens using Brush and Color objects with and without a specified width.

LISTING 4.15: Using the Pen class constructor to create Pen objects

private void Form1_Paint(object sender, PaintEventArgs e)
//Create a Graphics object and set it clear
            Graphics g = this.CreateGraphics();

//Create a solid brush and a hatch brush
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
HatchBrush hatchBrush = new HatchBrush(HatchStyle.DashedVertical, Color.Black, Color.Green);

//Create a pen from a solid brush with width 3
            Pen pn1 = new Pen(blueBrush, 3);

//Create a pen from a hatch brush
            Pen pn2 = new Pen(hatchBrush, 8);

//Create a pen from a Color structure
            Pen pn3 = new Pen(Color.Red);

//Draw a line, ellipse, and rectangle
            g.DrawLine(pn1, new Point(10, 40), new Point(10, 90));
            g.DrawEllipse(pn2, 20, 50, 100, 100);
            g.DrawRectangle(pn3, 40, 90, 100, 100);

//Dispose of objects

Figure 4.19 shows the output from Listing 4.15.

The Pens class has static properties for all standard colors, which return appropriately colored Pen objects. The following code snipped creates three Pen objects using the Pens class.

Figure 4.19.jpg

FIGURE 4.19: Creating a using pens

        Pen pn1 = Pens.Red;
        Pen pn2 = Pens.Blue;
        Pen pn3 = Pens.Green;

Pen Class Properties and Methods

The Pen class provides properties to set brush, color, and width programmatically after a Pen object is created. Table 4.8 describes the properties of the Pen class.

Table 4.9 describes the methods of the Pen class.

Pen Types

A pen can draw solid lines, filled lines, texture and even gradient lines – all depending on the brush you use to create the pen. For example, if you use a texture brush to create a pen and then use this pen to create lines, the lines will be texture lines.

The only way to set a pen's type is to create a brush and use that brush to create the pen. The PenType property of the Pen class represents the type of the pen's lines. This property is represented by the PenType enumeration.

Note: The PenType property is read-only property.

Table 4.10 describes the members of the PenType enumeration.

Pens Example

Now let's create a sample application. In Listing 4.16 we create three pens from three different brushes: a solid brush, a texture brush, and a linear gradient brush. After that we create three pens from these brushes, and then we read the type of each pen and display the types in a message box.

TABLE 4.8: Pen class properties




Alignment for a pen- a type of PenAlignment enumeration, which is defined in Table 4.11.


Brush object attached with a pen. Setting the Color property after Brush will replace the color of the current brush with the specified color.


Color of a pen. Setting the Brush property after Color will update the color of a pen to the color of the brush.


Specifies values of a compound pen, which draws compound lines made up of parallel lines and spaces.

CustomEndCap, CustomStartCap, DashCap

A line drawn by a pen can have custom staring and ending caps. The CustomEndCap and CutomerStartCap properties represent the ending and starting caps, respectively, of lines drawn by a pen. DashCap is used for dashed lines.


The distance from the start of a line to the beginning of a dash pattern.


An array of custom dashes and spaces.


The style used for dashed lines.

EndCap, StartCap

Ending and starting cap of a line.


The join style for the ends of two consecutive lines.


Limit of the thickness of the join on a mitered corner.


The style of lines of a pen. This property is represented by the PenType enumeration.


The geometric transformation of a pen.


The width of a pen.

TABLE 4.9: Pen class methods




Create an exact copy of a pen.


Multiplies the transformation matrix of a pen by Matrix.


Resets the geometric transformation by the specified angle.


Rotates the local geometric transformation by the specified angle.


Scales the local geometric transformation by the specified factors.


Sets the values that determine the style of cap used to end lines drawn by a pen.


Translates the local geometric transformation by the specified dimensions.

TABLE 4.10: PenType members




A hatch fill


A linear gradient fill


A path gradient fill


A solid fill


A bitmap texture fill

LISTING 4.16: Getting pen types

private void Form1_Paint(object sender, PaintEventArgs e)
//Create a Graphics object
            Graphics g = this.CreateGraphics();

//Create three different types of brushes
            Image img = new Bitmap("D:/VB.NET Basic Practice Projects/WindowsApplication8/Images/Image.GIF");
SolidBrush redBrush = new SolidBrush(Color.Red);
TextureBrush txtrBrush = new TextureBrush(img);
LinearGradientBrush lgBrush = new LinearGradientBrush( new Rectangle(10, 10, 10, 10),
Color.Red, Color.Black, 45.0f);

//Create pens from brushes
            Pen pn1 = new Pen(redBrush, 4);
Pen pn2 = new Pen(txtrBrush, 20);
Pen pn3 = new Pen(lgBrush, 20);

//Drawing objects
            g.DrawEllipse(pn1, 100, 100, 50, 50);
            g.DrawRectangle(pn2, 80, 80, 100, 100);
            g.DrawEllipse(pn3, 30, 30, 200, 200);

//Get pen types
            string str = "Pen1 Type: " +
                             pn1.PenType.ToString() +
            str +=
"Pen2 Type : " +
                             pn2.PenType.ToString() +
            str +=
"Pen3 Type : " +

//Dispose of objects

Figure 4.20 shows the output from Listing 4.16.

Figure 4.20.jpg

FIGURE 4.20: Displaying pen types

Hope the article would have helped you in understanding using Pens in GDI+. Read other articles on GDI+ on the website.

This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Up Next
    Ebook Download
    View all
    View all