Provides the size, in pixels, of the specified text when drawn with the specified font.
Namespace:
System.Windows.Forms
Assembly:
System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Declaration)
Public Shared Function MeasureText ( _
text As String, _
font As Font _
) As Size
Dim text As String
Dim font As Font
Dim returnValue As Size
returnValue = TextRenderer.MeasureText(text, _
font)
public static Size MeasureText(
string text,
Font font
)
public:
static Size MeasureText(
String^ text,
Font^ font
)
public static function MeasureText(
text : String,
font : Font
) : Size
The MeasureText method requires that the text is drawn on a single line.
The following code example demonstrates how to use the MeasureText method. To run this example, paste the code into a Windows Form and call MeasureText1 from the form's Paint event handler, passing e as PaintEventArgs.
Private Sub MeasureText1(ByVal e As PaintEventArgs)
Dim text1 As String = "Measure this text"
Dim arialBold As New Font("Arial", 12.0F)
Dim textSize As Size = TextRenderer.MeasureText(text1, arialBold)
TextRenderer.DrawText(e.Graphics, text1, arialBold, _
New Rectangle(New Point(10, 10), textSize), Color.Red)
End Sub
private void MeasureText1(PaintEventArgs e)
{
String text1 = "Measure this text";
Font arialBold = new Font("Arial", 12.0F);
Size textSize = TextRenderer.MeasureText(text1, arialBold);
TextRenderer.DrawText(e.Graphics, text1, arialBold,
new Rectangle(new Point(10, 10), textSize), Color.Red);
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0
Reference