System.Windows.Forms.TextRenderer.MeasureText Method

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

Syntax

public static System.Drawing.Size MeasureText (string text, System.Drawing.Font font)

Parameters

text
The text to measure.
font
The System.Drawing.Font to apply to the measured text.

Returns

The System.Drawing.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 TextRenderer.DrawText(System.Drawing.IDeviceContext, string, System.Drawing.Font, System.Drawing.Rectangle, System.Drawing.Color, TextFormatFlags) overloads that takes a System.Windows.Forms.TextFormatFlags parameter. For example, the default behavior of the System.Windows.Forms.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 TextRenderer.DrawText(System.Drawing.IDeviceContext, string, System.Drawing.Font, System.Drawing.Point, System.Drawing.Color) and TextRenderer.MeasureText(System.Drawing.IDeviceContext, string, System.Drawing.Font) that take a System.Drawing.Size and System.Windows.Forms.TextFormatFlags parameter. For an example, see TextRenderer.MeasureText(System.Drawing.IDeviceContext, string, System.Drawing.Font, System.Drawing.Size, TextFormatFlags).

Remarks

The TextRenderer.MeasureText(System.Drawing.IDeviceContext, string, System.Drawing.Font) method requires that the text is drawn on a single line.

Requirements

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Assembly Versions: 2.0.0.0
Since: .NET 2.0