Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public static Size MeasureText(
	string text,
	Font font,
	Size proposedSize
)

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.

Return Value

Type: System.Drawing.Size
The Size, in pixels, of text drawn with the specified font.

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.

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

}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.