Graphics.MeasureString 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
用指定的字符串绘制时度量指定的 Font字符串。
重载
MeasureString(ReadOnlySpan<Char>, Font, Int32, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的 Font 字符串绘制并用指定的格式绘制时度量指定的 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
参数
- text
- ReadOnlySpan<Char>
要度量的字符串。
- width
- Int32
字符串的最大宽度。
- format
- StringFormat
StringFormat 表示字符串的格式设置信息,如行距。
返回
此方法返回一个SizeF结构,该结构代表用参数和参数绘制font的参数中指定的text字符串的大小(以属性指定的PageUnit单位为单位format)。
适用于
MeasureString(String, Font, SizeF, StringFormat, Int32, Int32)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的 Font 字符串绘制并用指定的格式绘制时度量指定的 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
参数
- text
- String
要度量的字符串。
- stringFormat
- StringFormat
StringFormat 表示字符串的格式设置信息,如行距。
- charactersFitted
- Int32
字符串中的字符数。
- linesFilled
- Int32
字符串中的文本行数。
返回
此方法返回一个SizeF结构,该结构代表用参数和stringFormat参数绘制font的参数的单位(以属性text指定的PageUnit单位)字符串的大小。
例外
font 是 null。
示例
下面的代码示例设计用于 Windows Forms,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
创建用于度量的字符串,并将字体对象设置为 Arial (16 磅)
设置字符串的最大布局大小。
创建字符串格式对象并将其格式标志设置为 DirectionVertical。
创建整数变量
charactersFitted和linesFilled大小对象以度量字符串。度量字符串的大小,并确定使用字符串、字体对象、最大布局大小和字符串格式填充的字符数。
使用字符串的度量大小绘制红色矩形。
绘制矩形中的字符串。
绘制拟合字符数和填充行数的值。
结果是将垂直字符串括起来的垂直矩形。
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
注解
该方法 MeasureString 旨在与单个字符串一起使用,并在字符串前后包含少量的额外空间,以允许悬停字形。 此外,该方法 DrawString 调整字形点以优化显示质量,并可能显示比报告 MeasureString更窄的字符串。 若要获取适合布局中的相邻字符串的指标(例如,实现格式化文本时),请使用MeasureCharacterRanges该方法或采用StringFormat并传递GenericTypographic的方法之MeasureString一。 此外,请确保TextRenderingHint为 Graphics 。AntiAlias
另请参阅
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字体和文本
适用于
MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat, Int32, Int32)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的 Font 字符串绘制并用指定的格式绘制时度量指定的 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
参数
- text
- ReadOnlySpan<Char>
要度量的字符串。
- stringFormat
- StringFormat
StringFormat 表示字符串的格式设置信息,如行距。
- charactersFitted
- Int32
字符串中的字符数。
- linesFilled
- Int32
字符串中的文本行数。
返回
此方法返回一个SizeF结构,该结构代表用参数和stringFormat参数绘制font的参数的单位(以属性text指定的PageUnit单位)字符串的大小。
适用于
MeasureString(String, Font, Int32, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的 Font 字符串绘制并用指定的格式绘制时度量指定的 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
参数
- text
- String
要度量的字符串。
- width
- Int32
字符串的最大宽度。
- format
- StringFormat
StringFormat 表示字符串的格式设置信息,如行距。
返回
此方法返回一个SizeF结构,该结构代表用参数和参数绘制font的参数中指定的text字符串的大小(以属性指定的PageUnit单位为单位format)。
例外
font 是 null。
示例
下面的代码示例设计用于 Windows Forms,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
创建一个要度量的字符串,并将字体对象设置为 Arial(16 磅)。
设置字符串的最大宽度。
创建字符串格式对象并将其格式标志设置为 DirectionVertical。
创建一个大小对象来度量字符串。
使用字符串、字体对象、最大宽度和字符串格式度量字符串的大小。
使用字符串的度量大小绘制红色矩形。
绘制矩形中的字符串。
结果是将垂直字符串括起来的垂直矩形。
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
注解
该方法 MeasureString 旨在与单个字符串一起使用,并在字符串前后包含少量的额外空间,以允许悬停字形。 此外,该方法 DrawString 调整字形点以优化显示质量,并可能显示比报告 MeasureString更窄的字符串。 若要获取适合布局中相邻字符串的指标(例如,实现格式化文本时),请使用MeasureCharacterRanges该方法或采用的方法StringFormat之MeasureString一并传递GenericTypographic。 此外,请确保TextRenderingHint为 Graphics 。AntiAlias
另请参阅
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字体和文本
适用于
MeasureString(String, Font, SizeF, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的 Font 字符串绘制并用指定的格式绘制时度量指定的 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
参数
- text
- String
要度量的字符串。
- stringFormat
- StringFormat
StringFormat 表示字符串的格式设置信息,如行距。
返回
此方法返回一个SizeF结构,该结构代表用参数和参数绘制font的参数中指定的text字符串的大小(以属性指定的PageUnit单位为单位stringFormat)。
例外
font 是 null。
示例
下面的代码示例设计用于 Windows Forms,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
创建一个要度量的字符串,并将字体对象设置为 Arial (16 磅)。
设置字符串的最大布局大小,创建用于度量字符串的大小对象。
创建字符串格式对象并将其格式标志设置为 DirectionVertical。
使用字符串、字体对象、最大布局大小和字符串格式度量字符串的大小。
使用字符串的度量大小绘制红色矩形。
绘制矩形中的字符串。
结果是将垂直字符串括起来的垂直矩形。
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
注解
该方法 MeasureString 旨在与单个字符串一起使用,并在字符串前后包含少量的额外空间,以允许悬停字形。 此外,该方法 DrawString 调整字形点以优化显示质量,并可能显示比报告 MeasureString更窄的字符串。 若要获取适合布局中相邻字符串的指标(例如,实现格式化文本时),请使用MeasureCharacterRanges该方法或采用的方法StringFormat之MeasureString一并传递GenericTypographic。 此外,请确保TextRenderingHint为 Graphics 。AntiAlias
另请参阅
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字体和文本
适用于
MeasureString(String, Font, PointF, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的 Font 字符串绘制并用指定的格式绘制时度量指定的 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
参数
- text
- String
要度量的字符串。
- stringFormat
- StringFormat
StringFormat 表示字符串的格式设置信息,如行距。
返回
此方法返回一个SizeF结构,该结构表示由参数所指定的字符串的大小(以属性指定的PageUnit单位为单位),该字符串使用text参数和stringFormat参数绘制font。
例外
font 是 null。
示例
下面的代码示例设计用于 Windows Forms,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
创建用于度量的字符串,并将字体对象设置为 Arial (16 磅)
创建一个点以查找字符串的左上角。
创建字符串格式对象并将其格式标志设置为 DirectionVertical。
创建一个大小对象来度量字符串。
使用字符串、字体对象、定位点和字符串格式度量字符串的大小。
使用定位点和字符串的测量大小绘制红色矩形。
绘制矩形中的字符串。
结果是将垂直字符串括起来的垂直矩形。
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
注解
该方法 MeasureString 旨在与单个字符串一起使用,并在字符串前后包含少量的额外空间,以允许悬停字形。 此外,该方法 DrawString 调整字形点以优化显示质量,并可能显示比报告 MeasureString更窄的字符串。 若要获取适合布局中相邻字符串的指标(例如,实现格式化文本时),请使用MeasureCharacterRanges该方法或采用的方法StringFormat之MeasureString一并传递GenericTypographic。 此外,请确保TextRenderingHint为 Graphics 。AntiAlias
另请参阅
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字体和文本
适用于
MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的 Font 字符串绘制并用指定的格式绘制时度量指定的 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
参数
- text
- ReadOnlySpan<Char>
要度量的字符串。
- stringFormat
- StringFormat
StringFormat 表示字符串的格式设置信息,如行距。
返回
此方法返回一个SizeF结构,该结构代表用参数和参数绘制font的参数中指定的text字符串的大小(以属性指定的PageUnit单位为单位stringFormat)。
适用于
MeasureString(ReadOnlySpan<Char>, Font)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的字符串绘制时度量指定的 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
参数
- text
- ReadOnlySpan<Char>
要度量的字符串。
返回
此方法返回一个SizeF结构,该结构表示由参数指定的text字符串的大小(以属性指定的PageUnit单位为单位),font该字符串使用参数绘制。
适用于
MeasureString(String, Font, Int32)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的字符串绘制时度量指定的 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
参数
- text
- String
要度量的字符串。
- width
- Int32
字符串的最大宽度(以像素为单位)。
返回
此方法返回一个SizeF结构,该结构代表用参数绘制font的参数中指定的text字符串的大小(以属性指定的PageUnit单位为单位)。
例外
font 是 null。
示例
下面的代码示例设计用于 Windows Forms,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
创建一个要度量的字符串,并将字体对象设置为 Arial (16 磅)。
设置字符串的最大宽度。
创建一个大小对象并使用它、字体对象和最大字符串宽度来度量字符串的大小。
使用字符串的度量大小绘制红色矩形。
绘制矩形中的字符串。
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
注解
该 width 参数指定返回 SizeF 结构的Width宽度分量()的最大值。
width如果参数小于字符串的实际宽度,则Width返回的组件将被截断为一个值,表示将适合指定宽度的最大字符数。 为了容纳整个字符串,返回 Height 的组件将调整为一个值,该值允许显示带换行符的字符串。
该方法 MeasureString 旨在与单个字符串一起使用,并在字符串前后包含少量的额外空间,以允许悬停字形。 此外,该方法 DrawString 调整字形点以优化显示质量,并可能显示比报告 MeasureString更窄的字符串。 若要获取适合布局中相邻字符串的指标(例如,实现格式化文本时),请使用MeasureCharacterRanges该方法或采用的方法StringFormat之MeasureString一并传递GenericTypographic。 此外,请确保TextRenderingHint为 Graphics 。AntiAlias
另请参阅
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字体和文本
适用于
MeasureString(String, Font, SizeF)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定布局区域中的指定 Font 对象绘制时度量指定的字符串。
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
参数
- text
- String
要度量的字符串。
返回
此方法返回一个SizeF结构,该结构表示由参数指定的text字符串的大小(以属性指定的PageUnit单位为单位),font该字符串使用参数绘制。
例外
font 是 null。
示例
下面的代码示例设计用于 Windows Forms,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
创建一个要度量的字符串,并将字体对象设置为 Arial (16 磅)。
设置字符串的最大布局大小。
创建一个大小对象并使用它、字体对象和最大布局大小来度量字符串的大小。
使用字符串的度量大小绘制红色矩形。
绘制矩形中的字符串。
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
注解
该方法 MeasureString 旨在与单个字符串一起使用,并在字符串前后包含少量的额外空间,以允许悬停字形。 此外,该方法 DrawString 调整字形点以优化显示质量,并可能显示比报告 MeasureString更窄的字符串。 若要获取适合布局中相邻字符串的指标(例如,实现格式化文本时),请使用MeasureCharacterRanges该方法或采用的方法StringFormat之MeasureString一并传递GenericTypographic。 此外,请确保TextRenderingHint为 Graphics 。AntiAlias
另请参阅
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字体和文本
适用于
MeasureString(ReadOnlySpan<Char>, Font, Int32)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的字符串绘制时度量指定的 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
参数
- text
- ReadOnlySpan<Char>
要度量的字符串。
- width
- Int32
字符串的最大宽度(以像素为单位)。
返回
此方法返回一个SizeF结构,该结构代表用参数绘制font的参数中指定的text字符串的大小(以属性指定的PageUnit单位为单位)。
适用于
MeasureString(ReadOnlySpan<Char>, Font, SizeF)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定布局区域中的指定 Font 对象绘制时度量指定的字符串。
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
参数
- text
- ReadOnlySpan<Char>
要度量的字符串。
返回
此方法返回一个SizeF结构,该结构表示由参数指定的text字符串的大小(以属性指定的PageUnit单位为单位),font该字符串使用参数绘制。
适用于
MeasureString(String, Font)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的字符串绘制时度量指定的 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
参数
- text
- String
要度量的字符串。
返回
此方法返回一个SizeF结构,该结构表示由参数指定的text字符串的大小(以属性指定的PageUnit单位为单位),font该字符串使用参数绘制。
例外
font 是 null。
font 是 null。
示例
下面的代码示例设计用于 Windows Forms,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
创建要度量的字符串。
创建字体对象并将其设置为 Arial(16 磅)。
创建一个 size 对象,并使用它和字体对象来度量字符串的大小。
使用字符串的度量大小绘制红色矩形。
绘制矩形中的字符串。
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
注解
该方法 MeasureString 旨在与单个字符串一起使用,并在字符串前后包含少量的额外空间,以允许悬停字形。 此外,该方法 DrawString 调整字形点以优化显示质量,并可能显示比报告 MeasureString更窄的字符串。 若要获取适合布局中相邻字符串的指标(例如,实现格式化文本时),请使用MeasureCharacterRanges该方法或采用的方法StringFormat之MeasureString一并传递GenericTypographic。 此外,请确保TextRenderingHint为 Graphics 。AntiAlias
另请参阅
- MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)
- MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- 使用字体和文本
适用于
MeasureString(ReadOnlySpan<Char>, Font, PointF, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
用指定的 Font 字符串绘制并用指定的格式绘制时度量指定的 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
参数
- text
- ReadOnlySpan<Char>
要度量的字符串。
- stringFormat
- StringFormat
StringFormat 表示字符串的格式设置信息,如行距。
返回
此方法返回一个SizeF结构,该结构表示由参数所指定的字符串的大小(以属性指定的PageUnit单位为单位),该字符串使用text参数和stringFormat参数绘制font。