Export (0) Print
Expand All

Graphics.MeasureString Method (String, Font, PointF, StringFormat)

Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.

Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)

public SizeF MeasureString (
	string text,
	Font font,
	PointF origin,
	StringFormat stringFormat
)
public SizeF MeasureString (
	String text, 
	Font font, 
	PointF origin, 
	StringFormat stringFormat
)
public function MeasureString (
	text : String, 
	font : Font, 
	origin : PointF, 
	stringFormat : StringFormat
) : SizeF
Not applicable.

Parameters

text

String to measure.

font

Font defines the text format of the string.

origin

PointF structure that represents the upper-left corner of the string.

stringFormat

StringFormat that represents formatting information, such as line spacing, for the string.

Return Value

This method returns a SizeF structure that represents the size, in the units specified by the PageUnit property, of the string specified by the text parameter as drawn with the font parameter and the stringFormat parameter.

The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString. To obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.

The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:

  • Creates a string to measure and a font object set to Arial (16 point)

  • Creates a point to locate the upper-left corner of the string.

  • Creates a string format object and sets its format flags to DirectionVertical.

  • Creates a size object to measure the string.

  • Measures the size of the string, using the string, the font object, the locating point, and the string format.

  • Draws a red rectangle using the locating point and the measured size of the string.

  • Draws the string within the drawn rectangle.

The result is a vertical rectangle enclosing a vertical string.

private void MeasureStringPointFFormat(PaintEventArgs e)
{

    // Set up string.
    string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);

    // Set point for upper-left corner of string.
    float x = 50.0F;
    float y = 50.0F;
    PointF ulCorner = new PointF(x, y);

    // Set string format.
    StringFormat newStringFormat = new StringFormat();
    newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;

    // Measure string.
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont, ulCorner, newStringFormat);

    // Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), x, y, stringSize.Width, stringSize.Height);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, ulCorner, newStringFormat);
}

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft