Partager via


How to: Set Pen Width and Alignment

When you create a Pen, 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 0. 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 to determine how the pixels drawn with that pen will be positioned relative to theoretical lines.

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

The following code example draws a line twice: once with a black pen of width 1 and once with a green pen of width 10.

To vary the width of a pen

  • Set the value of the Alignment property to Center (the default) to specify that pixels drawn with the green pen will be centered on the theoretical line. The following illustration shows the resulting line.

    Pens

    The following code example draws a rectangle twice: once with a black pen of width 1 and once with a green pen of width 10.

    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)
    
    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);
    

To change the alignment of a pen

  • Set the value of the Alignment property to Center to specify that the pixels drawn with the green pen will be centered on the boundary of the rectangle.

    The following illustration shows the resulting rectangle.

    Pens

    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)
    
    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);
    

To create an inset pen

  • Change the green pen's alignment by modifying the third statement in the preceding code example as follows:

    greenPen.Alignment = PenAlignment.Inset
    
    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.

    Pens

See Also

Other Resources

Using a Pen to Draw Lines and Shapes
Graphics and Drawing in Windows Forms