System.Windows.Forms.TextRenderer.MeasureText Method

Provides the size, in pixels, of the specified text when drawn with the specified device context, font, and formatting instructions, using the specified size to create the initial bounding rectangle for the text.

Syntax

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

Parameters

dc
The device context in which to measure the text.
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

The TextRenderer.MeasureText(System.Drawing.IDeviceContext, string, System.Drawing.Font, System.Drawing.Size, TextFormatFlags) method 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, 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, as shown in the example.

Requirements

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