Graphics.MeasureString Method (String, Font, SizeF, StringFormat, Int32, Int32)
Assembly: System.Drawing (in System.Drawing.dll)
member MeasureString : text:string * font:Font * layoutArea:SizeF * stringFormat:StringFormat * charactersFitted:int byref * linesFilled:int byref -> SizeF
- Type: System.String
String to measure.
- Type: System.Drawing.SizeF
SizeF structure that specifies the maximum layout area for the text.
- Type: System.Drawing.StringFormat
StringFormat that represents formatting information, such as line spacing, for the string.
- Type: System.Int32
Number of characters in the string.
- Type: System.Int32
Number of text lines in the string.
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.
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.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.