Export (0) Print
Expand All

Graphics.MeasureString Method (String, Font, Int32)

.NET Framework 1.1

Measures the specified string when drawn with the specified Font object.

[Visual Basic]
Overloads Public Function MeasureString( _
   ByVal text As String, _
   ByVal font As Font, _
   ByVal width As Integer _
) As SizeF
[C#]
public SizeF MeasureString(
 string text,
 Font font,
 int width
);
[C++]
public: SizeF MeasureString(
 String* text,
 Font* font,
 int width
);
[JScript]
public function MeasureString(
   text : String,
 font : Font,
 width : int
) : SizeF;

Parameters

text
String to measure.
font
Font object that defines the format of the string.
width
Maximum width of the string in pixels.

Return Value

This method returns a SizeF structure that represents the size, in pixels, of the string specified in the text parameter as drawn with the font parameter.

Remarks

The width parameter specifies the maximum value of the width component of the returned SizeF structure (SizeF.Width). If the width parameter is less than the actual width of the string, the returned SizeF.Width component is truncated to a value representing the maximum number of characters that will fit within the specified width. To accommodate the entire string, the returned SizeF.Height component is adjusted to a value that allows displaying the string with character wrap.

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.

Example

[Visual Basic, C#] The following 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 (16pt.).
  • Sets the maximum width of the string.
  • Creates a size object and uses it, the font object, and the maximum string width 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.
[Visual Basic] 
Public Sub MeasureStringWidth(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 = 200
' Measure string.
Dim stringSize As New SizeF()
stringSize = e.Graphics.MeasureString(measureString, _
stringFont, stringWidth)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0F, 0F, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
New PointF(0, 0))
End Sub
        
[C#] 
public void MeasureStringWidth(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Set maximum width of string.
int stringWidth = 200;
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont, stringWidth);
// 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));
}
        

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Graphics Class | Graphics Members | System.Drawing Namespace | Graphics.MeasureString Overload List

Show:
© 2014 Microsoft