TextRenderer.MeasureText Method

Definition

Measures the specified text when drawn with the specified font.

Overloads

MeasureText(String, Font, Size, TextFormatFlags)

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

MeasureText(ReadOnlySpan<Char>, Font, Size, TextFormatFlags)

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

MeasureText(IDeviceContext, String, Font, Size)

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

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font, Size)

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

MeasureText(String, Font, Size)

Provides the size, in pixels, of the specified text when drawn with the specified font, using the specified size to create an initial bounding rectangle.

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font)

Provides the size, in pixels, of the specified text drawn with the specified font in the specified device context.

MeasureText(IDeviceContext, String, Font)

Provides the size, in pixels, of the specified text drawn with the specified font in the specified device context.

MeasureText(String, Font)

Provides the size, in pixels, of the specified text when drawn with the specified font.

MeasureText(ReadOnlySpan<Char>, Font)

Provides the size, in pixels, of the specified text when drawn with the specified font.

MeasureText(IDeviceContext, ReadOnlySpan<Char>, 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.

MeasureText(ReadOnlySpan<Char>, Font, Size)

Provides the size, in pixels, of the specified text when drawn with the specified font, using the specified size to create an initial bounding rectangle.

MeasureText(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.

MeasureText(String, Font, Size, TextFormatFlags)

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

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

public static System.Drawing.Size MeasureText (string text, System.Drawing.Font font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
public static System.Drawing.Size MeasureText (string? text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);

Parameters

text
String

The text to measure.

font
Font

The Font to apply to the measured text.

proposedSize
Size

The Size of the initial bounding rectangle.

flags
TextFormatFlags

The formatting instructions to apply to the measured text.

Returns

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

Examples

The following code example demonstrates how to use one of the MeasureText methods. To run this example, paste the code into a Windows Form and call DrawALineOfText from the form's Paint 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;
    }
}

Remarks

MeasureText 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 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).

Note

This overload of MeasureText(String, Font, Size, TextFormatFlags) will ignore a TextFormatFlags value of NoPadding or LeftAndRightPadding. If you are specifying a padding value other than the default, you should use the overload of MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags) that takes a IDeviceContext object.

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

MeasureText(ReadOnlySpan<Char>, Font, Size, TextFormatFlags)

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

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

public static System.Drawing.Size MeasureText (ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);

Parameters

text
ReadOnlySpan<Char>

The text to measure.

font
Font

The Font to apply to the measured text.

proposedSize
Size

The Size of the initial bounding rectangle.

flags
TextFormatFlags

The formatting instructions to apply to the measured text.

Returns

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

Exceptions

Applies to

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

MeasureText(IDeviceContext, String, Font, Size)

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

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

public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Size proposedSize);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Size proposedSize);

Parameters

dc
IDeviceContext

The device context in which to measure the text.

text
String

The text to measure.

font
Font

The Font to apply to the measured text.

proposedSize
Size

The Size of the initial bounding rectangle.

Returns

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

Exceptions

dc is null.

Examples

The following code example demonstrates how to use one of the MeasureText methods. To run this example, paste the code into a Windows Form and call DrawALineOfText from the form's Paint 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;
    }
}

Remarks

The MeasureText method uses the proposedSize parameter 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.

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

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font, Size)

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

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

public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize);

Parameters

dc
IDeviceContext

The device context in which to measure the text.

text
ReadOnlySpan<Char>

The text to measure.

font
Font

The Font to apply to the measured text.

proposedSize
Size

The Size of the initial bounding rectangle.

Returns

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

Exceptions

dc is null.

Applies to

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

MeasureText(String, Font, Size)

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

Provides the size, in pixels, of the specified text when drawn with the specified font, using the specified size to create an initial bounding rectangle.

public static System.Drawing.Size MeasureText (string text, System.Drawing.Font font, System.Drawing.Size proposedSize);
public static System.Drawing.Size MeasureText (string? text, System.Drawing.Font? font, System.Drawing.Size proposedSize);

Parameters

text
String

The text to measure.

font
Font

The Font to apply to the measured text.

proposedSize
Size

The Size of the initial bounding rectangle.

Returns

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

Examples

The following code example demonstrates how to use one of the MeasureText methods. To run this example, paste the code into a Windows Form and call DrawALineOfText from the form's Paint 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;
    }
}

Remarks

The MeasureText method uses the proposedSize parameter 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.

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

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font)

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

Provides the size, in pixels, of the specified text drawn with the specified font in the specified device context.

public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font);

Parameters

dc
IDeviceContext

The device context in which to measure the text.

text
ReadOnlySpan<Char>

The text to measure.

font
Font

The Font to apply to the measured text.

Returns

The Size, in pixels, of text drawn with the specified font in the specified device context.

Exceptions

dc is null.

Applies to

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

MeasureText(IDeviceContext, String, Font)

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

Provides the size, in pixels, of the specified text drawn with the specified font in the specified device context.

public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font);

Parameters

dc
IDeviceContext

The device context in which to measure the text.

text
String

The text to measure.

font
Font

The Font to apply to the measured text.

Returns

The Size, in pixels, of text drawn in a single line with the specified font in the specified device context.

Examples

The following code example demonstrates how to use one of the MeasureText methods. To run this example, paste the code into a Windows Form and call DrawALineOfText from the form's Paint 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;
    }
}

Remarks

The MeasureText method requires that the text is drawn on a single line.

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

MeasureText(String, Font)

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

Provides the size, in pixels, of the specified text when drawn with the specified font.

public static System.Drawing.Size MeasureText (string text, System.Drawing.Font font);
public static System.Drawing.Size MeasureText (string? text, System.Drawing.Font? font);

Parameters

text
String

The text to measure.

font
Font

The Font to apply to the measured text.

Returns

The Size, in pixels, of text drawn on a single line with the specified font. You can manipulate how the text is drawn by using one of the DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags) 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(IDeviceContext, String, Font, Point, Color) and MeasureText(IDeviceContext, String, Font) that take a Size and TextFormatFlags parameter. For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

Examples

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

private void MeasureText1(PaintEventArgs e)
{
    String text1 = "Measure this text";
    Font arialBold = new Font("Arial", 12.0F);
    Size textSize = TextRenderer.MeasureText(text1, arialBold);
    TextRenderer.DrawText(e.Graphics, text1, arialBold, 
        new Rectangle(new Point(10, 10), textSize), Color.Red);  
}

Remarks

The MeasureText method requires that the text is drawn on a single line.

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

MeasureText(ReadOnlySpan<Char>, Font)

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

Provides the size, in pixels, of the specified text when drawn with the specified font.

public static System.Drawing.Size MeasureText (ReadOnlySpan<char> text, System.Drawing.Font? font);

Parameters

text
ReadOnlySpan<Char>

The text to measure.

font
Font

The Font to apply to the measured text.

Returns

The Size, in pixels, of text drawn on a single line with the specified font. You can manipulate how the text is drawn by using one of the DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags) 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(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color) and MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font) that take a Size and TextFormatFlags parameter. For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

Applies to

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

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font, Size, TextFormatFlags)

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

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.

public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);

Parameters

dc
IDeviceContext

The device context in which to measure the text.

text
ReadOnlySpan<Char>

The text to measure.

font
Font

The Font to apply to the measured text.

proposedSize
Size

The Size of the initial bounding rectangle.

flags
TextFormatFlags

The formatting instructions to apply to the measured text.

Returns

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

Exceptions

dc is null.

Applies to

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

MeasureText(ReadOnlySpan<Char>, Font, Size)

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

Provides the size, in pixels, of the specified text when drawn with the specified font, using the specified size to create an initial bounding rectangle.

public static System.Drawing.Size MeasureText (ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize);

Parameters

text
ReadOnlySpan<Char>

The text to measure.

font
Font

The Font to apply to the measured text.

proposedSize
Size

The Size of the initial bounding rectangle.

Returns

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

Applies to

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

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

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

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.

public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);

Parameters

dc
IDeviceContext

The device context in which to measure the text.

text
String

The text to measure.

font
Font

The Font to apply to the measured text.

proposedSize
Size

The Size of the initial bounding rectangle.

flags
TextFormatFlags

The formatting instructions to apply to the measured text.

Returns

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

Exceptions

dc is null.

Examples

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's Paint 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;
    }
}

Remarks

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.

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