TextRenderer::MeasureText Method (String^, Font^)

 

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
)

Parameters

text
Type: System::String^

The text to measure.

font
Type: System.Drawing::Font^

The Font to apply to the measured text.

Return Value

Type: System.Drawing::Size

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.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Return to top
Show: