Graphics.MeasureString 方法

定义

用指定的 Font绘制时度量指定的字符串。

重载

MeasureString(ReadOnlySpan<Char>, Font, Int32, StringFormat)

用指定的 Font 绘制时度量指定的字符串,并使用指定的 StringFormat进行格式化。

MeasureString(String, Font, SizeF, StringFormat, Int32, Int32)

用指定的 Font 绘制时度量指定的字符串,并使用指定的 StringFormat进行格式化。

MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat, Int32, Int32)

用指定的 Font 绘制时度量指定的字符串,并使用指定的 StringFormat进行格式化。

MeasureString(String, Font, Int32, StringFormat)

用指定的 Font 绘制时度量指定的字符串,并使用指定的 StringFormat进行格式化。

MeasureString(String, Font, SizeF, StringFormat)

用指定的 Font 绘制时度量指定的字符串,并使用指定的 StringFormat进行格式化。

MeasureString(String, Font, PointF, StringFormat)

用指定的 Font 绘制时度量指定的字符串,并使用指定的 StringFormat进行格式化。

MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat)

用指定的 Font 绘制时度量指定的字符串,并使用指定的 StringFormat进行格式化。

MeasureString(ReadOnlySpan<Char>, Font)

用指定的 Font绘制时度量指定的字符串。

MeasureString(String, Font, Int32)

用指定的 Font绘制时度量指定的字符串。

MeasureString(String, Font, SizeF)

使用指定布局区域中的指定 Font 绘制时测量指定的字符串。

MeasureString(ReadOnlySpan<Char>, Font, Int32)

用指定的 Font绘制时度量指定的字符串。

MeasureString(ReadOnlySpan<Char>, Font, SizeF)

使用指定布局区域中的指定 Font 绘制时测量指定的字符串。

MeasureString(String, Font)

用指定的 Font绘制时度量指定的字符串。

MeasureString(ReadOnlySpan<Char>, Font, PointF, StringFormat)

用指定的 Font 绘制时度量指定的字符串,并使用指定的 StringFormat进行格式化。

MeasureString(ReadOnlySpan<Char>, Font, Int32, StringFormat)

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>

要度量的字符串。

font
Font

定义字符串文本格式的 Font

width
Int32

字符串的最大宽度。

format
StringFormat

表示字符串的格式信息(如行距)的 StringFormat

返回

此方法返回一个 SizeF 结构,该结构代表用 font 参数和 stringFormat 参数绘制的 text 参数中指定的字符串的大小(以 PageUnit 属性指定的单位)。

适用于

MeasureString(String, Font, SizeF, StringFormat, Int32, Int32)

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

要度量的字符串。

font
Font

定义字符串文本格式的 Font

layoutArea
SizeF

SizeF 结构,指定文本的最大布局区域。

stringFormat
StringFormat

表示字符串的格式信息(如行距)的 StringFormat

charactersFitted
Int32

字符串中的字符数。

linesFilled
Int32

字符串中的文本行数。

返回

此方法返回一个 SizeF 结构,该结构表示用 font 参数和 stringFormat 参数绘制的 text 参数 PageUnit 属性指定的字符串大小。

例外

font null

示例

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:

  • 创建用于度量的字符串,并将字体对象设置为 Arial (16 磅)

  • 设置字符串的最大布局大小。

  • 创建字符串格式对象并将其格式标志设置为 DirectionVertical

  • 创建整数变量 charactersFittedlinesFilled 以及用于度量字符串的大小对象。

  • 度量字符串的大小,并确定使用字符串、字体对象、最大布局大小和字符串格式填充的字符数。

  • 使用字符串的度量大小绘制红色矩形。

  • 绘制矩形中的字符串。

  • 绘制拟合字符数和填充行数的值。

结果是将垂直字符串括起来的垂直矩形。

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 并传递 GenericTypographicMeasureString 方法之一。 此外,请确保 GraphicsTextRenderingHintAntiAlias

另请参阅

适用于

MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat, Int32, Int32)

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>

要度量的字符串。

font
Font

定义字符串文本格式的 Font

layoutArea
SizeF

SizeF 结构,指定文本的最大布局区域。

stringFormat
StringFormat

表示字符串的格式信息(如行距)的 StringFormat

charactersFitted
Int32

字符串中的字符数。

linesFilled
Int32

字符串中的文本行数。

返回

此方法返回一个 SizeF 结构,该结构表示用 font 参数和 stringFormat 参数绘制的 text 参数 PageUnit 属性指定的字符串大小。

适用于

MeasureString(String, Font, Int32, StringFormat)

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

要度量的字符串。

font
Font

定义字符串文本格式的 Font

width
Int32

字符串的最大宽度。

format
StringFormat

表示字符串的格式信息(如行距)的 StringFormat

返回

此方法返回一个 SizeF 结构,该结构代表用 font 参数和 stringFormat 参数绘制的 text 参数中指定的字符串的大小(以 PageUnit 属性指定的单位)。

例外

font null

示例

下面的代码示例设计用于 Windows 窗体,它需要 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 方法或采用 StringFormatMeasureString 方法之一,并传递 GenericTypographic。 此外,请确保 GraphicsTextRenderingHintAntiAlias

另请参阅

适用于

MeasureString(String, Font, SizeF, StringFormat)

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

要度量的字符串。

font
Font

Font 定义字符串的文本格式。

layoutArea
SizeF

SizeF 结构,指定文本的最大布局区域。

stringFormat
StringFormat

表示字符串的格式信息(如行距)的 StringFormat

返回

此方法返回一个 SizeF 结构,该结构代表用 font 参数和 stringFormat 参数绘制的 text 参数中指定的字符串的大小(以 PageUnit 属性指定的单位)。

例外

font null

示例

下面的代码示例设计用于 Windows 窗体,它需要 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 方法或采用 StringFormatMeasureString 方法之一,并传递 GenericTypographic。 此外,请确保 GraphicsTextRenderingHintAntiAlias

另请参阅

适用于

MeasureString(String, Font, PointF, StringFormat)

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

要度量的字符串。

font
Font

Font 定义字符串的文本格式。

origin
PointF

PointF 表示字符串左上角的结构。

stringFormat
StringFormat

表示字符串的格式信息(如行距)的 StringFormat

返回

此方法返回一个 SizeF 结构,该结构表示由 text 参数指定的字符串的大小(以 PageUnit 属性指定的单位)作为 font 参数和 stringFormat 参数绘制。

例外

font null

示例

下面的代码示例设计用于 Windows 窗体,它需要 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 方法或采用 StringFormatMeasureString 方法之一,并传递 GenericTypographic。 此外,请确保 GraphicsTextRenderingHintAntiAlias

另请参阅

适用于

MeasureString(ReadOnlySpan<Char>, Font, SizeF, StringFormat)

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>

要度量的字符串。

font
Font

Font 定义字符串的文本格式。

layoutArea
SizeF

SizeF 结构,指定文本的最大布局区域。

stringFormat
StringFormat

表示字符串的格式信息(如行距)的 StringFormat

返回

此方法返回一个 SizeF 结构,该结构代表用 font 参数和 stringFormat 参数绘制的 text 参数中指定的字符串的大小(以 PageUnit 属性指定的单位)。

适用于

MeasureString(ReadOnlySpan<Char>, Font)

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>

要度量的字符串。

font
Font

定义字符串文本格式的 Font

返回

此方法返回一个 SizeF 结构,该结构代表用 font 参数绘制的由 text 参数指定的字符串的大小(以 PageUnit 属性指定的单位)。

适用于

MeasureString(String, Font, Int32)

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

要度量的字符串。

font
Font

定义字符串格式的 Font

width
Int32

字符串的最大宽度(以像素为单位)。

返回

此方法返回一个 SizeF 结构,该结构代表用 font 参数绘制的 text 参数中指定的字符串的大小(以 PageUnit 属性指定的单位)。

例外

font null

示例

下面的代码示例设计用于 Windows 窗体,它需要 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 方法或采用 StringFormatMeasureString 方法之一,并传递 GenericTypographic。 此外,请确保 GraphicsTextRenderingHintAntiAlias

另请参阅

适用于

MeasureString(String, Font, SizeF)

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

要度量的字符串。

font
Font

Font 定义字符串的文本格式。

layoutArea
SizeF

SizeF 结构,指定文本的最大布局区域。

返回

此方法返回一个 SizeF 结构,该结构代表用 font 参数绘制的由 text 参数指定的字符串的大小(以 PageUnit 属性指定的单位)。

例外

font null

示例

下面的代码示例设计用于 Windows 窗体,它需要 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 方法或采用 StringFormatMeasureString 方法之一,并传递 GenericTypographic。 此外,请确保 GraphicsTextRenderingHintAntiAlias

另请参阅

适用于

MeasureString(ReadOnlySpan<Char>, Font, Int32)

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>

要度量的字符串。

font
Font

定义字符串格式的 Font

width
Int32

字符串的最大宽度(以像素为单位)。

返回

此方法返回一个 SizeF 结构,该结构代表用 font 参数绘制的 text 参数中指定的字符串的大小(以 PageUnit 属性指定的单位)。

适用于

MeasureString(ReadOnlySpan<Char>, Font, SizeF)

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>

要度量的字符串。

font
Font

Font 定义字符串的文本格式。

layoutArea
SizeF

SizeF 结构,指定文本的最大布局区域。

返回

此方法返回一个 SizeF 结构,该结构代表用 font 参数绘制的由 text 参数指定的字符串的大小(以 PageUnit 属性指定的单位)。

适用于

MeasureString(String, Font)

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

要度量的字符串。

font
Font

定义字符串文本格式的 Font

返回

此方法返回一个 SizeF 结构,该结构代表用 font 参数绘制的由 text 参数指定的字符串的大小(以 PageUnit 属性指定的单位)。

例外

font null

font null

示例

下面的代码示例设计用于 Windows 窗体,它需要 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 方法或采用 StringFormatMeasureString 方法之一,并传递 GenericTypographic。 此外,请确保 GraphicsTextRenderingHintAntiAlias

另请参阅

适用于

MeasureString(ReadOnlySpan<Char>, Font, PointF, StringFormat)

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>

要度量的字符串。

font
Font

Font 定义字符串的文本格式。

origin
PointF

PointF 表示字符串左上角的结构。

stringFormat
StringFormat

表示字符串的格式信息(如行距)的 StringFormat

返回

此方法返回一个 SizeF 结构,该结构表示由 text 参数指定的字符串的大小(以 PageUnit 属性指定的单位)作为 font 参数和 stringFormat 参数绘制。

适用于