Edit

Share via


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)

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(System::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);
public static System.Drawing.Size MeasureText (string? text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
static member MeasureText : string * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size
Public Shared Function MeasureText (text As String, font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

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;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

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

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.

public:
 static System::Drawing::Size MeasureText(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize, System::Windows::Forms::TextFormatFlags flags);
public static System.Drawing.Size MeasureText (ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
static member MeasureText : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size
Public Shared Function MeasureText (text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

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

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.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, System::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);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Size proposedSize);
static member MeasureText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As String, font As Font, proposedSize As Size) As Size

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;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

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

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.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize);
static member MeasureText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size) As Size

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

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.

public:
 static System::Drawing::Size MeasureText(System::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);
public static System.Drawing.Size MeasureText (string? text, System.Drawing.Font? font, System.Drawing.Size proposedSize);
static member MeasureText : string * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size
Public Shared Function MeasureText (text As String, font As Font, proposedSize As Size) As Size

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;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

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

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font)

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);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font);
static member MeasureText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font) As Size

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

MeasureText(IDeviceContext, String, Font)

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, System::String ^ text, System::Drawing::Font ^ font);
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);
static member MeasureText : System.Drawing.IDeviceContext * string * System.Drawing.Font -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As String, font As Font) As Size

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;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

Remarks

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

Applies to

MeasureText(String, Font)

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

public:
 static System::Drawing::Size MeasureText(System::String ^ text, System::Drawing::Font ^ 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);
static member MeasureText : string * System.Drawing.Font -> System.Drawing.Size
Public Shared Function MeasureText (text As String, font As Font) As Size

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);  
}
Private Sub MeasureText1(ByVal e As PaintEventArgs)
    Dim text1 As String = "Measure this text"
    Dim arialBold As New Font("Arial", 12.0F)
    Dim textSize As Size = TextRenderer.MeasureText(text1, arialBold)
    TextRenderer.DrawText(e.Graphics, text1, arialBold, _
        New Rectangle(New Point(10, 10), textSize), Color.Red)

End Sub

Remarks

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

Applies to

MeasureText(ReadOnlySpan<Char>, Font)

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);
public static System.Drawing.Size MeasureText (ReadOnlySpan<char> text, System.Drawing.Font? font);
static member MeasureText : ReadOnlySpan<char> * System.Drawing.Font -> System.Drawing.Size
Public Shared Function MeasureText (text As ReadOnlySpan(Of Char), font As Font) As Size

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

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.

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);
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);
static member MeasureText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

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

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.

public:
 static System::Drawing::Size MeasureText(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize);
public static System.Drawing.Size MeasureText (ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize);
static member MeasureText : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size
Public Shared Function MeasureText (text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size) As Size

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

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.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, System::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);
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);
static member MeasureText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As String, font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

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;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

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