logo
CSharp_Graphics

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

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

How to: Draw Text at a Specified Location

When you perform custom drawing, you can draw text in a single horizontal line starting at a specified point. You can draw text in this manner by using the DrawString overloaded method of the Graphics class that takes a Point or PointF parameter. The DrawString method also requires a Brush and Font

You can also use the DrawText overloaded method of the TextRenderer that takes a Point. DrawText also requires a Color and a Font.

The following illustration shows the output of text drawn at a specified point when you use the DrawString overloaded method.

To draw a line of text with GDI+

using (Font font1 = new Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel))

{

PointF pointF1 = new PointF(30, 10);

e.Graphics.DrawString("Hello", font1, Brushes.Blue, pointF1);

}

To draw a line of text with GDI

using (Font font = new Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel))

{

Point point1 = new Point(30, 10);

TextRenderer.DrawText(e.Graphics, "Hello", font, point1, Color.Blue);

}

Compiling the Code

The previous examples require: