Graphics.MeasureString Method (String, Font, Int32, 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 Function MeasureString (
	text As String,
	font As Font,
	width As Integer,
	format As StringFormat
) As SizeF


Type: System.String

String to measure.

Type: System.Drawing.Font

Font that defines the text format of the string.

Type: System.Int32

Maximum width of the string.

Type: System.Drawing.StringFormat

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

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 in the text parameter as drawn with the font parameter and the stringFormat parameter.

Exception Condition

font is null.

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 it to Arial (16 point).

  • Sets the maximum width of the string.

  • Creates a string format object and sets its format flags toDirectionVertical.

  • Creates a size object to measure the string.

  • Measures the size of the string, using the string, the font object, the maximum width, and the string format.

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

  • Draws the string within the drawn rectangle.

The result is a vertical rectangle enclosing a vertical string.

Private Sub MeasureStringWidthFormat(ByVal e As PaintEventArgs)

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

    ' Set maximum width of string.
    Dim stringWidth As Integer = 100

    ' Set string format.
    Dim newStringFormat As New StringFormat
    newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical

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

    ' 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), newStringFormat)
End Sub

.NET Framework
Available since 1.1
Return to top