Share via


Setting Pen Width and Alignment

When you create a Pen object, you can supply the pen width as one of the arguments to the constructor. You can also change the pen width with the Width property of the Pen class.

A theoretical line has a width of zero. When you draw a line that is 1 pixel wide, the pixels are centered on the theoretical line. If you draw a line that is more than one pixel wide, the pixels are either centered on the theoretical line or appear to one side of the theoretical line. You can set the pen alignment property of a Pen object to determine how the pixels drawn with that pen will be positioned relative to theoretical lines.

The following example draws a line twice: once with a black pen of width 1 and once with a green pen of width 10. The code sets the value of the Alignment property to PenAlignment.Center (the default) to specify that pixels drawn with the green pen will be centered on the theoretical line:

Dim blackPen As New Pen(Color.FromArgb(255, 0, 0, 0), 1)
Dim greenPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10)
greenPen.Alignment = PenAlignment.Center

' Draw the line with the wide green pen.
e.Graphics.DrawLine(greenPen, 10, 100, 100, 50)

' Draw the line with the thin black pen.
e.Graphics.DrawLine(blackPen, 10, 100, 100, 50)
[C#]
Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
greenPen.Alignment = PenAlignment.Center;

// Draw the line with the wide green pen.
e.Graphics.DrawLine(greenPen, 10, 100, 100, 50);

// Draw the line with the thin black pen.
e.Graphics.DrawLine(blackPen, 10, 100, 100, 50);

The following illustration shows the resulting line.

3bssbs7z.pens1a(en-us,VS.71).gif

The following example draws a rectangle twice: once with a black pen of width 1 and once with a green pen of width 10. The code sets the value of the Alignment property to PenAlignment.Center to specify that the pixels drawn with the green pen will be centered on the boundary of the rectangle:

Dim blackPen As New Pen(Color.FromArgb(255, 0, 0, 0), 1)
Dim greenPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10)
greenPen.Alignment = PenAlignment.Center

' Draw the rectangle with the wide green pen.
e.Graphics.DrawRectangle(greenPen, 10, 100, 50, 50)

' Draw the rectangle with the thin black pen.
e.Graphics.DrawRectangle(blackPen, 10, 100, 50, 50)
[C#]
Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
greenPen.Alignment = PenAlignment.Center;

// Draw the rectangle with the wide green pen.
e.Graphics.DrawRectangle(greenPen, 10, 100, 50, 50);

// Draw the rectangle with the thin black pen.
e.Graphics.DrawRectangle(blackPen, 10, 100, 50, 50);

The following illustration shows the resulting rectangle.

3bssbs7z.pens2(en-us,VS.71).gif

You can change the green pen's alignment by modifying the third statement in the preceding code as follows:

greenPen.Alignment = PenAlignment.Inset
[C#]
greenPen.Alignment = PenAlignment.Inset;

Now the pixels in the wide green line appear on the inside of the rectangle as shown in the following illustration.

3bssbs7z.pens3(en-us,VS.71).gif

The values PenAlignment.Center, PenAlignment.Outset, and PenAlignment.Inset that appear in the preceding examples are members of the PenAlignment enumeration.