MeasureText Method (IDeviceContext, String, Font, Size, TextFormatFlags)

TextRenderer.MeasureText Method (IDeviceContext, String, Font, Size, TextFormatFlags)


Provides the size, in pixels, of the specified text when drawn with the specified device context, font, and formatting instructions, using the specified size to create the initial bounding rectangle for the text.

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

public static Size MeasureText(
	IDeviceContext dc,
	string text,
	Font font,
	Size proposedSize,
	TextFormatFlags flags


Type: System.Drawing.IDeviceContext

The device context in which to measure the text.

Type: System.String

The text to measure.

Type: System.Drawing.Font

The Font to apply to the measured text.

Type: System.Drawing.Size

The Size of the initial bounding rectangle.

Type: System.Windows.Forms.TextFormatFlags

The formatting instructions to apply to the measured text.

Return Value

Type: System.Drawing.Size

The Size, in pixels, of text drawn with the specified font and format.

Exception Condition

dc is null.

The MeasureText method uses the proposedSize and flags parameters to indicate the relationship of height to width when determining the text size. When measuring text on a single line, if the proposedSize parameter represents a Size with a height dimension greater than Int32.MaxValue, the returned Size will be adjusted to reflect the actual height of the text.

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, use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter, as shown in the example.

The following example demonstrates how to use the MeasureText and DrawText methods to draw a single line of text in different font styles. To run this example paste the following code in a Windows Form and call DrawALineOfText from the form'sPaint event handler, passing e as PaintEventArgs.

private static void DrawALineOfText(PaintEventArgs e)
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;


.NET Framework
Available since 2.0
Return to top
© 2015 Microsoft