Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Graphics.MeasureString Method (String, Font, SizeF)

Measures the specified string when drawn with the specified Font within the specified layout area.

Namespace:  System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)
'Declaration
Public Function MeasureString ( _
	text As String, _
	font As Font, _
	layoutArea As SizeF _
) As SizeF

Parameters

text
Type: System.String

String to measure.

font
Type: System.Drawing.Font

Font defines the text format of the string.

layoutArea
Type: System.Drawing.SizeF

SizeF structure that specifies the maximum layout area for the text.

Return Value

Type: System.Drawing.SizeF
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.
ExceptionCondition
ArgumentException

font is Nothing.

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 size object and uses it, the font object, and the maximum layout size to measure the size of the string.

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

  • Draws the string within the drawn rectangle.

Private Sub MeasureStringSizeF(ByVal e As PaintEventArgs)

    ' Set up string. 
    Dim measureString As String = "Measure String" 
    Dim stringFont As New Font("Arial", 16)

    ' Set maximum layout size. 
    Dim layoutSize As New SizeF(200.0F, 50.0F)

    ' Measure string. 
    Dim stringSize As New SizeF
    stringSize = e.Graphics.MeasureString(measureString, stringFont, _
    layoutSize)

    ' Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
    stringSize.Width, stringSize.Height)

    ' Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
    New PointF(0, 0))
End Sub

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.