.NET Framework Class Library
TextRenderer..::.MeasureText Method (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.

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

Visual Basic (Declaration)
Public Shared Function MeasureText ( _
    text As String, _
    font As Font, _
    proposedSize As Size, _
    flags As TextFormatFlags _
) As Size
Visual Basic (Usage)
Dim text As String
Dim font As Font
Dim proposedSize As Size
Dim flags As TextFormatFlags
Dim returnValue As Size

returnValue = TextRenderer.MeasureText(text, _
    font, proposedSize, flags)
C#
public static Size MeasureText(
    string text,
    Font font,
    Size proposedSize,
    TextFormatFlags flags
)
Visual C++
public:
static Size MeasureText(
    String^ text, 
    Font^ font, 
    Size proposedSize, 
    TextFormatFlags flags
)
JScript
public static function MeasureText(
    text : String, 
    font : Font, 
    proposedSize : Size, 
    flags : TextFormatFlags
) : Size

Parameters

text
Type: System..::.String
The text to measure.
font
Type: System.Drawing..::.Font
The Font to apply to the measured text.
proposedSize
Type: System.Drawing..::.Size
The Size of the initial bounding rectangle.
flags
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.
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).

NoteNote:

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.

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.

Visual Basic
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
C#
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;
    }

}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Tags :


Page view tracker