TextRenderer.DrawText Method

Definition

Draws the specified text at the specified location, using the specified device context, color, and font.

Overloads

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color)

Draws the specified text at the specified location using the specified device context, font, and color.

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color, TextFormatFlags)

Draws the specified text within the specified bounds using the specified device context, font, color, back color, and formatting instructions.

DrawText(IDeviceContext, String, Font, Point, Color, Color, TextFormatFlags)

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

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color, TextFormatFlags)

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

DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags)

Draws the specified text within the specified bounds using the specified device context, font, color, and formatting instructions.

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color)

Draws the specified text within the specified bounds using the specified device context, font, color, and back color.

DrawText(IDeviceContext, String, Font, Point, Color, TextFormatFlags)

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

DrawText(IDeviceContext, String, Font, Point, Color, Color)

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

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, Color, TextFormatFlags)

Draws the specified text within the specified bounds using the specified device context, font, color, back color, and formatting instructions.

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, Color)

Draws the specified text within the specified bounds using the specified device context, font, color, and back color.

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, TextFormatFlags)

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

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color)

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

DrawText(IDeviceContext, String, Font, Rectangle, Color)

Draws the specified text within the specified bounds, using the specified device context, font, and color.

DrawText(IDeviceContext, String, Font, Point, Color)

Draws the specified text at the specified location using the specified device context, font, and color.

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color)

Draws the specified text within the specified bounds, using the specified device context, font, and color.

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags)

Draws the specified text within the specified bounds using the specified device context, font, color, and formatting instructions.

Remarks

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.

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

Draws the specified text at the specified location using the specified device context, font, and color.

public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

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

foreColor
Color

The Color to apply to the drawn text.

Exceptions

dc is null.

Applies to

Windows Desktop 9 and other versions
Product Versions
Windows Desktop 5, 6, 7, 8, 9

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color, TextFormatFlags)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

Draws the specified text within the specified bounds using the specified device context, font, color, back color, and formatting instructions.

public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the text.

backColor
Color

The Color to apply to the area represented by bounds.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Examples

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

private void RenderText8(PaintEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Bottom | TextFormatFlags.WordBreak;
    TextRenderer.DrawText(e.Graphics, "This is some text that will display on multiple lines.", this.Font,
        new Rectangle(10, 10, 100, 50), SystemColors.ControlText, SystemColors.ControlDark, flags);
}

Remarks

The backcolor parameter is applied to the area within the bounds parameter. If font, forecolor or backcolor 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.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawText(IDeviceContext, String, Font, Point, Color, Color, TextFormatFlags)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

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

public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

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

foreColor
Color

The Color to apply to the text.

backColor
Color

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

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Examples

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

Remarks

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.

Note

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.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color, TextFormatFlags)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

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

public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

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

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

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

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Applies to

Windows Desktop 9 and other versions
Product Versions
Windows Desktop 5, 6, 7, 8, 9

DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

Draws the specified text within the specified bounds using the specified device context, font, color, and formatting instructions.

public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Examples

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

private void RenderText6(PaintEventArgs e)
{
    TextFormatFlags flags = TextFormatFlags.Bottom | TextFormatFlags.EndEllipsis;
    TextRenderer.DrawText(e.Graphics, "This is some text that will be clipped at the end.", this.Font,
        new Rectangle(10, 10, 100, 50), SystemColors.ControlText, flags);
}

Remarks

If font or color 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.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawText(IDeviceContext, String, Font, Rectangle, Color, Color)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

Draws the specified text within the specified bounds using the specified device context, font, color, and back color.

public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

The Color to apply to the area represented by bounds.

Exceptions

dc is null.

Examples

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

private void RenderText4(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text.", this.Font,
        new Rectangle(10, 10, 70, 70), SystemColors.ControlText, 
        SystemColors.ControlDark);
}

Remarks

The backColor parameter is applied to the area within the bounds parameter. If font, backcolor 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.

This method will result in text that is horizontally and vertically centered in the rectangle specified by the bounds parameter. To change how the text is drawn, use a version of DrawText that takes a TextFormatFlags parameter type.

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.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawText(IDeviceContext, String, Font, Point, Color, TextFormatFlags)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

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

public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

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

foreColor
Color

The Color to apply to the drawn text.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Examples

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

private void RenderText5(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Some text.",
        this.Font, new Point(10, 10), SystemColors.ControlText, TextFormatFlags.Bottom);
}

Remarks

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 backColor parameter is applied to the area within the bounds parameter. If font or forecolor is null or Color.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.

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.

Note

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.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawText(IDeviceContext, String, Font, Point, Color, Color)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

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

public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

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

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

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

Exceptions

dc is null.

Examples

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

private void RenderText3(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
        new Point(10, 10), Color.Red, Color.PowderBlue);
}

Remarks

The backColor parameter is applied to the rectangular area containing the drawn text. If font, backcolor or forecolor is null or Color.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.

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.

Note

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.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, Color, TextFormatFlags)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

Draws the specified text within the specified bounds using the specified device context, font, color, back color, and formatting instructions.

public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor, System.Windows.Forms.TextFormatFlags flags);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

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

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Applies to

Windows Desktop 9 and other versions
Product Versions
Windows Desktop 5, 6, 7, 8, 9

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, Color)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

Draws the specified text within the specified bounds using the specified device context, font, color, and back color.

public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Drawing.Color backColor);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

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

Exceptions

dc is null.

Applies to

Windows Desktop 9 and other versions
Product Versions
Windows Desktop 5, 6, 7, 8, 9

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, TextFormatFlags)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

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

public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

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

foreColor
Color

The Color to apply to the drawn text.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Applies to

Windows Desktop 9 and other versions
Product Versions
Windows Desktop 5, 6, 7, 8, 9

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color, Color)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

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

public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor, System.Drawing.Color backColor);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

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

foreColor
Color

The Color to apply to the drawn text.

backColor
Color

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

Exceptions

dc is null.

Applies to

Windows Desktop 9 and other versions
Product Versions
Windows Desktop 5, 6, 7, 8, 9

DrawText(IDeviceContext, String, Font, Rectangle, Color)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

Draws the specified text within the specified bounds, using the specified device context, font, and color.

public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

Exceptions

dc is null.

Examples

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

private void RenderText2(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font,
        new Rectangle(10, 10, 100, 100), SystemColors.ControlText);
}

Remarks

If font or forecolor is null or Color.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.

This method will result in text that is horizontally and vertically centered in the rectangle specified by the bounds parameter. To change how the text is drawn, use a version of DrawText that takes a TextFormatFlags parameter type.

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawText(IDeviceContext, String, Font, Point, Color)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

Draws the specified text at the specified location using the specified device context, font, and color.

public static void DrawText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Point pt, System.Drawing.Color foreColor);
public static void DrawText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Point pt, System.Drawing.Color foreColor);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
String

The text to draw.

font
Font

The Font to apply to the drawn text.

pt
Point

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

foreColor
Color

The Color to apply to the drawn text.

Exceptions

dc is null.

Examples

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

private void RenderText1(PaintEventArgs e)
{
    TextRenderer.DrawText(e.Graphics, "Regular Text", this.Font, 
        new Point(10, 10), SystemColors.ControlText);
}

Remarks

You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter.

If font is null or forecolor is Color.Empty, 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.

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.

Note

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.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

Draws the specified text within the specified bounds, using the specified device context, font, and color.

public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

Exceptions

dc is null.

Applies to

Windows Desktop 9 and other versions
Product Versions
Windows Desktop 5, 6, 7, 8, 9

DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags)

Source:
TextRenderer.cs
Source:
TextRenderer.cs
Source:
TextRenderer.cs

Draws the specified text within the specified bounds using the specified device context, font, color, and formatting instructions.

public static void DrawText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Rectangle bounds, System.Drawing.Color foreColor, System.Windows.Forms.TextFormatFlags flags);

Parameters

dc
IDeviceContext

The device context in which to draw the text.

text
ReadOnlySpan<Char>

The text to draw.

font
Font

The Font to apply to the drawn text.

bounds
Rectangle

The Rectangle that represents the bounds of the text.

foreColor
Color

The Color to apply to the drawn text.

flags
TextFormatFlags

A bitwise combination of the TextFormatFlags values.

Exceptions

dc is null.

Applies to

Windows Desktop 9 and other versions
Product Versions
Windows Desktop 5, 6, 7, 8, 9