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

TextRenderer.MeasureText Method (String, Font)

Note: This method is new in the .NET Framework version 2.0.

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

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public static Size MeasureText (
	string text,
	Font font
)
public static Size MeasureText (
	String text, 
	Font font
)
public static function MeasureText (
	text : String, 
	font : Font
) : Size

Parameters

text

The text to measure.

font

The Font to apply to the measured text.

Return Value

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

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

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.