Graphics.MeasureString Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Measures the specified string when drawn with the specified Font.
Overloads
MeasureString(ReadOnlySpan<Char>, Font, Int32, StringFormat) |
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat. |
MeasureString(String, Font, SizeF, StringFormat, Int32, Int32) |
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat. |
MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat, Int32, Int32) |
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat. |
MeasureString(String, Font, Int32, StringFormat) |
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat. |
MeasureString(String, Font, SizeF, StringFormat) |
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat. |
MeasureString(String, Font, PointF, StringFormat) |
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat. |
MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat) |
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat. |
MeasureString(ReadOnlySpan<Char>, Font) |
Measures the specified string when drawn with the specified Font. |
MeasureString(String, Font, Int32) |
Measures the specified string when drawn with the specified Font. |
MeasureString(String, Font, SizeF) |
Measures the specified string when drawn with the specified Font within the specified layout area. |
MeasureString(ReadOnlySpan<Char>, Font, Int32) |
Measures the specified string when drawn with the specified Font. |
MeasureString(ReadOnlySpan<Char>, Font, SizeF) |
Measures the specified string when drawn with the specified Font within the specified layout area. |
MeasureString(String, Font) |
Measures the specified string when drawn with the specified Font. |
MeasureString(ReadOnlySpan<Char>, Font, PointF, StringFormat) |
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat. |
MeasureString(ReadOnlySpan<Char>, Font, Int32, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, int width, System::Drawing::StringFormat ^ format);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, int width, System.Drawing.StringFormat? format);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * int * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, width As Integer, format As StringFormat) As SizeF
Parameters
- text
- ReadOnlySpan<Char>
String to measure.
- width
- Int32
Maximum width of the string.
- format
- StringFormat
StringFormat that represents formatting information, such as line spacing, for the string.
Returns
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.
Applies to
MeasureString(String, Font, SizeF, StringFormat, Int32, Int32)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea, System::Drawing::StringFormat ^ stringFormat, [Runtime::InteropServices::Out] int % charactersFitted, [Runtime::InteropServices::Out] int % linesFilled);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat? stringFormat, out int charactersFitted, out int linesFilled);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat stringFormat, out int charactersFitted, out int linesFilled);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.SizeF * System.Drawing.StringFormat * int * int -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, layoutArea As SizeF, stringFormat As StringFormat, ByRef charactersFitted As Integer, ByRef linesFilled As Integer) As SizeF
Parameters
- text
- String
String to measure.
- stringFormat
- StringFormat
StringFormat that represents formatting information, such as line spacing, for the string.
- charactersFitted
- Int32
Number of characters in the string.
- linesFilled
- Int32
Number of text lines in the string.
Returns
This method returns a SizeF structure that represents the size of the string, in the units specified by the PageUnit property, of the text
parameter as drawn with the font
parameter and the stringFormat
parameter.
Exceptions
font
is null
.
Examples
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 string format object and sets its format flags to DirectionVertical.
Creates the integer variables
charactersFitted
andlinesFilled
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.
The result is a vertical rectangle enclosing a vertical string.
public:
void MeasureStringSizeFFormatInts( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set maximum layout size.
SizeF layoutSize = SizeF(100.0F,200.0F);
// Set string format.
StringFormat^ newStringFormat = gcnew StringFormat;
newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;
// Measure string.
int charactersFitted;
int linesFilled;
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, layoutSize, newStringFormat, charactersFitted, linesFilled );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0), newStringFormat );
// Draw output parameters to screen.
String^ outString = String::Format( "chars {0}, lines {1}", charactersFitted, linesFilled );
e->Graphics->DrawString( outString, stringFont, Brushes::Black, PointF(100,0) );
}
private 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));
}
Private Sub MeasureStringSizeFFormatInts(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(100.0F, 200.0F)
' 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), 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.
Dim outString As String = "chars " & charactersFitted & _
", lines " & linesFilled
e.Graphics.DrawString(outString, stringFont, Brushes.Black, _
New PointF(100, 0))
End Sub
Remarks
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.
See also
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- Using Fonts and Text
Applies to
MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat, Int32, Int32)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea, System::Drawing::StringFormat ^ stringFormat, [Runtime::InteropServices::Out] int % charactersFitted, [Runtime::InteropServices::Out] int % linesFilled);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat? stringFormat, out int charactersFitted, out int linesFilled);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.SizeF * System.Drawing.StringFormat * int * int -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, layoutArea As SizeF, stringFormat As StringFormat, ByRef charactersFitted As Integer, ByRef linesFilled As Integer) As SizeF
Parameters
- text
- ReadOnlySpan<Char>
String to measure.
- stringFormat
- StringFormat
StringFormat that represents formatting information, such as line spacing, for the string.
- charactersFitted
- Int32
Number of characters in the string.
- linesFilled
- Int32
Number of text lines in the string.
Returns
This method returns a SizeF structure that represents the size of the string, in the units specified by the PageUnit property, of the text
parameter as drawn with the font
parameter and the stringFormat
parameter.
Applies to
MeasureString(String, Font, Int32, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, int width, System::Drawing::StringFormat ^ format);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, int width, System.Drawing.StringFormat? format);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, int width, System.Drawing.StringFormat format);
member this.MeasureString : string * System.Drawing.Font * int * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, width As Integer, format As StringFormat) As SizeF
Parameters
- text
- String
String to measure.
- width
- Int32
Maximum width of the string.
- format
- StringFormat
StringFormat that represents formatting information, such as line spacing, for the string.
Returns
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.
Exceptions
font
is null
.
Examples
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 it to Arial (16 point).
Sets the maximum width of the string.
Creates a string format object and sets its format flags to DirectionVertical.
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.
public:
void MeasureStringWidthFormat( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set maximum width of string.
int stringWidth = 100;
// Set string format.
StringFormat^ newStringFormat = gcnew StringFormat;
newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, stringWidth, newStringFormat );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0), newStringFormat );
}
private void MeasureStringWidthFormat(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Set maximum width of string.
int stringWidth = 100;
// Set string format.
StringFormat newStringFormat = new StringFormat();
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// Measure string.
SizeF stringSize = 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);
}
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
Remarks
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.
See also
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- Using Fonts and Text
Applies to
MeasureString(String, Font, SizeF, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat? stringFormat);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat stringFormat);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.SizeF * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, layoutArea As SizeF, stringFormat As StringFormat) As SizeF
Parameters
- text
- String
String to measure.
- stringFormat
- StringFormat
StringFormat that represents formatting information, such as line spacing, for the string.
Returns
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.
Exceptions
font
is null
.
Examples
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, creating a size object to measure the string.
Creates a string format object and sets its format flags to DirectionVertical.
Measures the size of the string, 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.
The result is a vertical rectangle enclosing a vertical string.
public:
void MeasureStringSizeFFormat( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set maximum layout size.
SizeF layoutSize = SizeF(100.0F,200.0F);
// Set string format.
StringFormat^ newStringFormat = gcnew StringFormat;
newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, layoutSize, newStringFormat );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0), newStringFormat );
}
private void MeasureStringSizeFFormat(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.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont, layoutSize, 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);
}
Private Sub MeasureStringSizeFFormat(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(100.0F, 200.0F)
' 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, _
layoutSize, 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
Remarks
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.
See also
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- Using Fonts and Text
Applies to
MeasureString(String, Font, PointF, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::PointF origin, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, System.Drawing.PointF origin, System.Drawing.StringFormat? stringFormat);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.PointF origin, System.Drawing.StringFormat stringFormat);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.PointF * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, origin As PointF, stringFormat As StringFormat) As SizeF
Parameters
- text
- String
String to measure.
- stringFormat
- StringFormat
StringFormat that represents formatting information, such as line spacing, for the string.
Returns
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 and the stringFormat
parameter.
Exceptions
font
is null
.
Examples
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)
Creates a point to locate the upper-left corner of the string.
Creates a string format object and sets its format flags to DirectionVertical.
Creates a size object to measure the string.
Measures the size of the string, using the string, the font object, the locating point, and the string format.
Draws a red rectangle using the locating point and the measured size of the string.
Draws the string within the drawn rectangle.
The result is a vertical rectangle enclosing a vertical string.
public:
void MeasureStringPointFFormat( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set point for upper-left corner of string.
float x = 50.0F;
float y = 50.0F;
PointF ulCorner = PointF(x,y);
// Set string format.
StringFormat^ newStringFormat = gcnew StringFormat;
newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, ulCorner, newStringFormat );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), x, y, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, ulCorner, newStringFormat );
}
private void MeasureStringPointFFormat(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Set point for upper-left corner of string.
float x = 50.0F;
float y = 50.0F;
PointF ulCorner = new PointF(x, y);
// Set string format.
StringFormat newStringFormat = new StringFormat();
newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont, ulCorner, newStringFormat);
// Draw rectangle representing size of string.
e.Graphics.DrawRectangle(new Pen(Color.Red, 1), x, y, stringSize.Width, stringSize.Height);
// Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, ulCorner, newStringFormat);
}
Private Sub MeasureStringPointFFormat(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Set point for upper-left corner of string.
Dim x As Single = 50.0F
Dim y As Single = 50.0F
Dim ulCorner As New PointF(x, y)
' 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, _
ulCorner, newStringFormat)
' Draw rectangle representing size of string.
e.Graphics.DrawRectangle(New Pen(Color.Red, 1), x, y, _
stringSize.Width, stringSize.Height)
' Draw string to screen.
e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
ulCorner, newStringFormat)
End Sub
Remarks
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.
See also
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- Using Fonts and Text
Applies to
MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat? stringFormat);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.SizeF * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, layoutArea As SizeF, stringFormat As StringFormat) As SizeF
Parameters
- text
- ReadOnlySpan<Char>
String to measure.
- stringFormat
- StringFormat
StringFormat that represents formatting information, such as line spacing, for the string.
Returns
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.
Applies to
MeasureString(ReadOnlySpan<Char>, Font)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font.
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font) As SizeF
Parameters
- text
- ReadOnlySpan<Char>
String to measure.
Returns
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.
Applies to
MeasureString(String, Font, Int32)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font.
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, int width);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, int width);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, int width);
member this.MeasureString : string * System.Drawing.Font * int -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, width As Integer) As SizeF
Parameters
- text
- String
String to measure.
- width
- Int32
Maximum width of the string in pixels.
Returns
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.
Exceptions
font
is null
.
Examples
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 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.
public:
void MeasureStringWidth( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set maximum width of string.
int stringWidth = 200;
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, stringWidth );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0) );
}
private 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));
}
Private Sub MeasureStringWidth(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 = 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), 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
Remarks
The width
parameter specifies the maximum value of the width component of the returned SizeF structure (Width). If the width
parameter is less than the actual width of the string, the returned 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 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 or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. Also, ensure the TextRenderingHint for the Graphics is AntiAlias.
See also
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- Using Fonts and Text
Applies to
MeasureString(String, Font, SizeF)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font within the specified layout area.
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font, System.Drawing.SizeF layoutArea);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.SizeF layoutArea);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.SizeF -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font, layoutArea As SizeF) As SizeF
Parameters
- text
- String
String to measure.
Returns
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.
Exceptions
font
is null
.
Examples
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.
public:
void MeasureStringSizeF( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Set maximum layout size.
SizeF layoutSize = SizeF(200.0F,50.0F);
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, layoutSize );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0) );
}
private void MeasureStringSizeF(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Set maximum layout size.
SizeF layoutSize = new SizeF(200.0F, 50.0F);
// Measure string.
SizeF stringSize = 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));
}
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
Remarks
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.
See also
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- Using Fonts and Text
Applies to
MeasureString(ReadOnlySpan<Char>, Font, Int32)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font.
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, int width);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, int width);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * int -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, width As Integer) As SizeF
Parameters
- text
- ReadOnlySpan<Char>
String to measure.
- width
- Int32
Maximum width of the string in pixels.
Returns
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.
Applies to
MeasureString(ReadOnlySpan<Char>, Font, SizeF)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font within the specified layout area.
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.SizeF layoutArea);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.SizeF -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, layoutArea As SizeF) As SizeF
Parameters
- text
- ReadOnlySpan<Char>
String to measure.
Returns
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.
Applies to
MeasureString(String, Font)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font.
public:
System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font);
public System.Drawing.SizeF MeasureString (string? text, System.Drawing.Font font);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font);
member this.MeasureString : string * System.Drawing.Font -> System.Drawing.SizeF
Public Function MeasureString (text As String, font As Font) As SizeF
Parameters
- text
- String
String to measure.
Returns
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.
Exceptions
font
is null
.
font
is null
.
Examples
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.
Creates a font object and sets it to Arial (16 point).
Creates a size object and uses it and the font object 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.
public:
void MeasureStringMin( PaintEventArgs^ e )
{
// Set up string.
String^ measureString = "Measure String";
System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );
// Measure string.
SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont );
// Draw rectangle representing size of string.
e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );
// Draw string to screen.
e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0) );
}
private void MeasureStringMin(PaintEventArgs e)
{
// Set up string.
string measureString = "Measure String";
Font stringFont = new Font("Arial", 16);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = e.Graphics.MeasureString(measureString, stringFont);
// 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));
}
Private Sub MeasureStringMin(ByVal e As PaintEventArgs)
' Set up string.
Dim measureString As String = "Measure String"
Dim stringFont As New Font("Arial", 16)
' Measure string.
Dim stringSize As New SizeF
stringSize = e.Graphics.MeasureString(measureString, stringFont)
' 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
Remarks
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.
See also
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- Using Fonts and Text
Applies to
MeasureString(ReadOnlySpan<Char>, Font, PointF, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.
public:
System::Drawing::SizeF MeasureString(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::PointF origin, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.SizeF MeasureString (ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.PointF origin, System.Drawing.StringFormat? stringFormat);
member this.MeasureString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.PointF * System.Drawing.StringFormat -> System.Drawing.SizeF
Public Function MeasureString (text As ReadOnlySpan(Of Char), font As Font, origin As PointF, stringFormat As StringFormat) As SizeF
Parameters
- text
- ReadOnlySpan<Char>
String to measure.
- stringFormat
- StringFormat
StringFormat that represents formatting information, such as line spacing, for the string.
Returns
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 and the stringFormat
parameter.
Applies to
.NET