Graphics::MeasureString Method (String, Font, SizeF, StringFormat, Int32%, Int32%)
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.
Assembly: System.Drawing (in System.Drawing.dll)
public: SizeF MeasureString( String^ text, Font^ font, SizeF layoutArea, StringFormat^ stringFormat, [OutAttribute] int% charactersFitted, [OutAttribute] int% linesFilled )
Parameters
- text
- Type: System::String
String to measure.
- font
- Type: System.Drawing::Font
Font that defines the text format of the string.
- layoutArea
- Type: System.Drawing::SizeF
SizeF structure that specifies the maximum layout area for the text.
- stringFormat
- Type: System.Drawing::StringFormat
StringFormat that represents formatting information, such as line spacing, for the string.
- charactersFitted
- Type: System::Int32%
Number of characters in the string.
- linesFilled
- Type: System::Int32%
Number of text lines in the string.
Return Value
Type: System.Drawing::SizeFThis method returns a SizeF structure that represents the size of the string, in the units specified by the PageUnit property, of 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 PaintEventArgs e, 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)
Sets the maximum layout size of the string.
Creates a string format object and sets its format flags to DirectionVertical.
Creates the integer variables charactersFitted and linesFilled and a size object to measure the string.
Measures the size of the string and determines the number of characters fitted and lines filled, using the string, the font object, the maximum layout size, and the string format.
Draws a red rectangle using the measured size of the string.
Draws the string within the drawn rectangle.
Draws the values of the number of characters fitted and lines filled.
The result is a vertical rectangle enclosing a vertical string.
public: void MeasureStringSizeFFormatInts( PaintEventArgs^ e ) { // Set up string. String^ measureString = "Measure String"; System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 ); // Set maximum layout size. SizeF layoutSize = SizeF(100.0F,200.0F); // Set string format. StringFormat^ newStringFormat = gcnew StringFormat; newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical; // Measure string. int charactersFitted; int linesFilled; SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, layoutSize, newStringFormat, charactersFitted, linesFilled ); // Draw rectangle representing size of string. e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height ); // Draw string to screen. e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0), newStringFormat ); // Draw output parameters to screen. String^ outString = String::Format( "chars {0}, lines {1}", charactersFitted, linesFilled ); e->Graphics->DrawString( outString, stringFont, Brushes::Black, PointF(100,0) ); }
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.