TextRenderer.DrawText Method (IDeviceContext, String, Font, Point, Color, Color, TextFormatFlags)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Draws the specified text at the specified location using the specified device context, font, color, back color, and formatting instructions

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public static void DrawText(
	IDeviceContext dc,
	string text,
	Font font,
	Point pt,
	Color foreColor,
	Color backColor,
	TextFormatFlags flags


Type: System.Drawing.IDeviceContext

The device context in which to draw the text.

Type: System.String

The text to draw.

Type: System.Drawing.Font

The Font to apply to the drawn text.

Type: System.Drawing.Point

The Point that represents the upper-left corner of the drawn text.

Type: System.Drawing.Color

The Color to apply to the text.

Type: System.Drawing.Color

The Color to apply to the background area of the drawn text.

Type: System.Windows.Forms.TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exception Condition

dc is null.

The backcolor parameter is applied the rectangular area containing the drawn text. If font or forecolor is null or Empty, respectively; the DrawText method will draw the text in the font or color currently selected in the device context specified by dc. If forecolor is Transparent, the text will not be drawn.

You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. If you need to draw a line of text without these extra spaces you should use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter. For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

The text rendering offered by the TextRenderer class is based on GDI text rendering and is not supported for printing from Windows Forms. Instead, use the DrawString methods of the Graphics class.

The TextRenderer does not support adding tab stops to drawn text, although you can expand existing tab stops using the ExpandTabs flag.


The DrawText methods that specify a Point as the upper-left corner of the drawn text do not render correctly on Windows 2000. If your application is intended for use on machines running Windows 2000, you should use one of the DrawText methods that specify a Rectangle for the bounds of the drawn text.

The following code example demonstrates how to use the DrawText method. To run this example, paste the code into a Windows Form and call RenderText7 from the form's Paint event handler, passing e as PaintEventArgs.

private void RenderText7(PaintEventArgs e)
    TextRenderer.DrawText(e.Graphics, "This is some text.", this.Font,
        new Point(10, 10), Color.White, Color.SteelBlue, TextFormatFlags.Default);

.NET Framework
Available since 2.0
Return to top