System.Windows.Forms.TextRenderer.MeasureText Method

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.

Syntax

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

Parameters

text
The text to measure.
font
The System.Drawing.Font to apply to the measured text.
proposedSize
The System.Drawing.Size of the initial bounding rectangle.
flags
The formatting instructions to apply to the measured text.

Returns

The System.Drawing.Size, in pixels, of text drawn with the specified font and format.

Remarks

TextRenderer.MeasureText(string, System.Drawing.Font, System.Drawing.Size) 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 System.Drawing.Size with a height dimension greater than int.MaxValue, the returned System.Drawing.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 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).

Note:

This overload of TextRenderer.MeasureText(string, System.Drawing.Font, System.Drawing.Size, TextFormatFlags) will ignore a System.Windows.Forms.TextFormatFlags value of TextFormatFlags.NoPadding or TextFormatFlags.LeftAndRightPadding. If you are specifying a padding value other than the default, you should use the overload of TextRenderer.MeasureText(System.Drawing.IDeviceContext, string, System.Drawing.Font, System.Drawing.Size, TextFormatFlags) that takes a System.Drawing.IDeviceContext object.

Requirements

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