Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp_Graphics.doc
Скачиваний:
16
Добавлен:
16.11.2019
Размер:
3.1 Mб
Скачать

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

  • Создайте форму Windows Forms и перейдите к обработчику события Paint этой формы. Вставьте приведенный пример кода в обработчик события Paint и передайте e в качестве объекта PaintEventArgs.

How to: Join Lines

A line join is the common area that is formed by two lines whose ends meet or overlap. GDI+ provides three line join styles: miter, bevel, and round. Line join style is a property of the Pen class. When you specify a line join style for a Pen object, that join style will be applied to all the connected lines in any GraphicsPath object drawn using that pen.

The following illustration shows the results of the beveled line join example.

Example

You can specify the line join style by using the LineJoin property of the Pen class. The example demonstrates a beveled line join between a horizontal line and a vertical line. In the following code, the value Bevel assigned to the LineJoin property is a member of the LineJoin enumeration. The other members of the LineJoin enumeration are Miter and Round.

GraphicsPath path = new GraphicsPath();

Pen penJoin = new Pen(Color.FromArgb(255, 0, 0, 255), 8);

path.StartFigure();

path.AddLine(new Point(50, 200), new Point(100, 200));

path.AddLine(new Point(100, 200), new Point(100, 250));

penJoin.LineJoin = LineJoin.Bevel;

e.Graphics.DrawPath(penJoin, path);

Compiling the Code

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

Соединение линий

Соединение линий — это общая область, образуемая двумя линиями с соприкасающимися или пересекающимися концами. Интерфейс GDI+ предоставляет три стиля соединения линий: фацетное соединение, скошенное соединение и скругленное соединение. Стиль соединения линий является свойством класса Pen. После задания стиля соединения линий для объекта Pen этот стиль будет применяться ко всем соединенным линиям любого объекта GraphicsPath, для рисования которого используется данное перо.

Результат скошенного соединения линий показан на следующем рисунке.

Пример

Для указания стиля соединения линий служит свойство LineJoin класса Pen. В примере демонстрируется использование скошенного соединения горизонтальной и вертикальной линий. В следующем фрагменте кода значение Bevel, присвоенное свойству LineJoin является членом перечисления LineJoin. Другие члены перечисления LineJoin — Miter и Round.

-------

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

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

How to: Draw a Custom Dashed Line

GDI+ provides several dash styles that are listed in the DashStyle enumeration. If those standard dash styles do not suit your needs, you can create a custom dash pattern.

Example

To draw a custom dashed line, put the lengths of the dashes and spaces in an array and assign the array as the value of the DashPattern property of a Pen object. The following example draws a custom dashed line based on the array {5, 2, 15, 4}. If you multiply the elements of the array by the pen width of 5, you get {25, 10, 75, 20}. The displayed dashes alternate in length between 25 and 75, and the spaces alternate in length between 10 and 20.

The following illustration shows the resulting dashed line. Note that the final dash has to be shorter than 25 units so that the line can end at (405, 5).

float[] dashValues = { 5, 2, 15, 4 };

Pen blackPen = new Pen(Color.Black, 5);

blackPen.DashPattern = dashValues;

e.Graphics.DrawLine(blackPen, new Point(5, 5), new Point(405, 5));

Compiling the Code

Create a Windows Form and handle the form's Paint event. Paste the preceding code into the Paint event handler