Drawing Text

You can use the DrawString method of the Graphics class to draw text at a specified location or within a specified rectangle.

Drawing Text at a Specified Location

To draw text at a specified location, you need Graphics, FontFamily, Font, PointF, and Brush objects.

The following example draws the string "Hello" at location (30, 10). The font family is Times New Roman. The font, which is an individual member of the font family, is Times New Roman, size 24 pixels, regular style.

Dim fontFamily As New FontFamily("Times New Roman")
Dim font As New Font(fontFamily, 24, FontStyle.Bold, GraphicsUnit.Pixel)
Dim pointF As New PointF(30, 10)
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))

e.Graphics.DrawString("Hello", font, solidBrush, pointF)
[C#]
FontFamily fontFamily = new FontFamily("Times New Roman");
Font font = new Font(fontFamily, 24, FontStyle.Bold, GraphicsUnit.Pixel);
PointF pointF = new PointF(30, 10);
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));

e.Graphics.DrawString("Hello", font, solidBrush, pointF);

The following illustration shows the output of the preceding code.

b87ey14t.csfontstext1(en-us,VS.71).gif

In the preceding example, the FontFamily constructor receives a string that identifies the font family. The FontFamily object is passed as the first argument to the Font constructor. The second argument passed to the Font constructor specifies the size of the font measured in units given by the fourth argument. The third argument specifies the style (such as regular, bold, or italic) of the font.

The DrawString method receives four arguments. The first argument is the string to be drawn. The second argument is the Font object that was constructed previously. The third argument is a SolidBrush object that is used to fill the characters of the string. The fourth argument is a PointF object that contains the coordinates of the upper-left corner of the string.

Drawing Text in a Rectangle

One of the DrawString methods of the Graphics class takes a RectangleF parameter. By calling that DrawString method, you can draw text that wraps in a specified rectangle. To draw text in a rectangle, you need Graphics, FontFamily, Font, RectangleF, and Brush objects.

The following example creates a rectangle with upper-left corner (30, 10), width 100, and height 122. Then the code draws a string inside that rectangle. The string is restricted to the rectangle and wraps without breaking words.

Dim myText As String = "Draw text in a rectangle by passing a RectangleF to the DrawString method."

Dim fontFamily As New FontFamily("Arial")
Dim font As New Font( _
   fontFamily, _
   12, _
   FontStyle.Bold, _
   GraphicsUnit.Point)
Dim rect As New Rectangle(30, 10, 100, 122)
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))

e.Graphics.DrawString(myText, font, solidBrush, _
   RectangleF.op_implicit(rect))

Dim pen As Pen = Pens.Black
e.Graphics.DrawRectangle(pen, rect)
[C#]
string text = "Draw text in a rectangle by passing a RectangleF to the DrawString method.";

FontFamily fontFamily = new FontFamily("Arial");
Font font = new Font(
   fontFamily,
   12,
   FontStyle.Bold,
   GraphicsUnit.Point);
Rectangle rect = new Rectangle(30, 10, 100, 122);
SolidBrush solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));

e.Graphics.DrawString(text, font, solidBrush, rect); 

Pen pen = Pens.Black;
e.Graphics.DrawRectangle(pen, rect);  

The following illustration shows the text drawn in the rectangle.

b87ey14t.csfontstext2(en-us,VS.71).gif

In the preceding example, the fourth argument passed to the DrawString method is a RectangleF object that specifies the bounding rectangle for the text.