Export (0) Print
Expand All
1 out of 8 rated this helpful - Rate this topic

Graphics.MeasureString Method

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

Overload List

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

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function MeasureString(String, Font) As SizeF
[C#] public SizeF MeasureString(string, Font);
[C++] public: SizeF MeasureString(String*, Font*);
[JScript] public function MeasureString(String, Font) : SizeF;

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

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

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

[Visual Basic] Overloads Public Function MeasureString(String, Font, SizeF) As SizeF
[C#] public SizeF MeasureString(string, Font, SizeF);
[C++] public: SizeF MeasureString(String*, Font*, SizeF);
[JScript] public function MeasureString(String, Font, SizeF) : SizeF;

Measures the specified string when drawn with the specified Font object and formatted with the specified StringFormat object.

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

Measures the specified string when drawn with the specified Font object and formatted with the specified StringFormat object.

[Visual Basic] Overloads Public Function MeasureString(String, Font, PointF, StringFormat) As SizeF
[C#] public SizeF MeasureString(string, Font, PointF, StringFormat);
[C++] public: SizeF MeasureString(String*, Font*, PointF, StringFormat*);
[JScript] public function MeasureString(String, Font, PointF, StringFormat) : SizeF;

Measures the specified string when drawn with the specified Font object and formatted with the specified StringFormat object.

[Visual Basic] Overloads Public Function MeasureString(String, Font, SizeF, StringFormat) As SizeF
[C#] public SizeF MeasureString(string, Font, SizeF, StringFormat);
[C++] public: SizeF MeasureString(String*, Font*, SizeF, StringFormat*);
[JScript] public function MeasureString(String, Font, SizeF, StringFormat) : SizeF;

Measures the specified string when drawn with the specified Font object and formatted with the specified StringFormat object.

[Visual Basic] Overloads Public Function MeasureString(String, Font, SizeF, StringFormat, ByRef Integer, ByRef Integer) As SizeF
[C#] public SizeF MeasureString(string, Font, SizeF, StringFormat, int, int);
[C++] public: SizeF MeasureString(String*, Font*, SizeF, StringFormat*, int, int);
[JScript] public function MeasureString(String, Font, SizeF, StringFormat, int, int) : SizeF;

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 layout size of the string.
  • Creates a string format object and sets its format flags to StringFormatFlags.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.

[Visual Basic, C#] The result is a vertical rectangle enclosing a vertical string.

[Visual Basic, C#] Note   This example shows how to use one of the overloaded versions of MeasureString. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Sub MeasureStringSizeFFormatInts(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(100F, 200F)
' Set string format.
Dim newStringFormat As New StringFormat()
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical
' Measure string.
Dim charactersFitted As Integer
Dim linesFilled As Integer
Dim stringSize As New SizeF()
stringSize = e.Graphics.MeasureString(measureString, stringFont, _
layoutSize, newStringFormat, charactersFitted, linesFilled)
' 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), newStringFormat)
' Draw output parameters to screen.
Dim outString As String = "chars " & charactersFitted & _
", lines " & linesFilled
e.Graphics.DrawString(outString, stringFont, Brushes.Black, _
New PointF(100, 0))
End Sub
        
[C#] 
public void MeasureStringSizeFFormatInts(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Set maximum layout size.
SizeF layoutSize = new SizeF(100.0F, 200.0F);
// Set string format.
StringFormat newStringFormat = new StringFormat();
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// Measure string.
int charactersFitted;
int linesFilled;
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(
measureString,
stringFont,
layoutSize,
newStringFormat,
out charactersFitted,
out linesFilled);
// 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);
// Draw output parameters to screen.
string outString = "chars " + charactersFitted + ", lines " + linesFilled;
e.Graphics.DrawString(
outString,
stringFont,
Brushes.Black,
new PointF(100, 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.

See Also

Graphics Class | Graphics Members | System.Drawing Namespace

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