logo
CSharp_Graphics

Компиляция кода

Предыдущий пример предназначен для работы с Windows Forms, для него необходим объект PaintEventArgs e, передаваемый в качестве параметра обработчику события Paint.

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

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.

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