Partager via


Graphics.MeasureString Méthode

Définition

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Fontspécifiée.

Surcharges

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

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

MeasureString(ReadOnlySpan<Char>, Font)

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Fontspécifiée.

MeasureString(String, Font)

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Fontspécifiée.

MeasureString(ReadOnlySpan<Char>, Font, SizeF)

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée dans la zone de disposition spécifiée.

MeasureString(ReadOnlySpan<Char>, Font, Int32)

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Fontspécifiée.

MeasureString(String, Font, SizeF)

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée dans la zone de disposition spécifiée.

MeasureString(String, Font, Int32)

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Fontspécifiée.

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

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

MeasureString(String, Font, PointF, StringFormat)

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

MeasureString(String, Font, SizeF, StringFormat)

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

MeasureString(String, Font, Int32, StringFormat)

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Paramètres

text
ReadOnlySpan<Char>

Chaîne à mesurer.

font
Font

Font définit le format de texte de la chaîne.

layoutArea
SizeF

SizeF structure qui spécifie la zone de disposition maximale du texte.

stringFormat
StringFormat

StringFormat qui représente des informations de mise en forme, telles que l’interligne, pour la chaîne.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée dans le paramètre text comme dessiné avec le paramètre font et le paramètre stringFormat.

S’applique à

MeasureString(ReadOnlySpan<Char>, Font)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Fontspécifiée.

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

Paramètres

text
ReadOnlySpan<Char>

Chaîne à mesurer.

font
Font

Font qui définit le format de texte de la chaîne.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée par le paramètre text comme dessiné avec le paramètre font.

S’applique à

MeasureString(String, Font)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Fontspécifiée.

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

Paramètres

text
String

Chaîne à mesurer.

font
Font

Font qui définit le format de texte de la chaîne.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée par le paramètre text comme dessiné avec le paramètre font.

Exceptions

font est null.

font est null.

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre du gestionnaire d’événements Paint. Le code effectue les actions suivantes :

  • Crée une chaîne à mesurer.

  • Crée un objet de police et le définit sur Arial (16 points).

  • Crée un objet de taille et l’utilise et l’objet de police pour mesurer la taille de la chaîne.

  • Dessine un rectangle rouge à l’aide de la taille mesurée de la chaîne.

  • Dessine la chaîne dans le rectangle dessiné.

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

Remarques

La méthode MeasureString est conçue pour être utilisée avec des chaînes individuelles et inclut une petite quantité d’espace supplémentaire avant et après la chaîne pour permettre des glyphes en surplomb. En outre, la méthode DrawString ajuste les points de glyphe pour optimiser la qualité de l’affichage et peut afficher une chaîne plus étroite que celle signalée par MeasureString. Pour obtenir des métriques adaptées aux chaînes adjacentes dans la disposition (par exemple, lors de l’implémentation du texte mis en forme), utilisez la méthode MeasureCharacterRanges ou l’une des méthodes MeasureString qui accepte un StringFormatet passez GenericTypographic. Vérifiez également que la TextRenderingHint pour la Graphics est AntiAlias.

Voir aussi

S’applique à

MeasureString(ReadOnlySpan<Char>, Font, SizeF)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée dans la zone de disposition spécifiée.

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

Paramètres

text
ReadOnlySpan<Char>

Chaîne à mesurer.

font
Font

Font définit le format de texte de la chaîne.

layoutArea
SizeF

SizeF structure qui spécifie la zone de disposition maximale du texte.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée par le paramètre text comme dessiné avec le paramètre font.

S’applique à

MeasureString(ReadOnlySpan<Char>, Font, Int32)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Fontspécifiée.

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

Paramètres

text
ReadOnlySpan<Char>

Chaîne à mesurer.

font
Font

Font qui définit le format de la chaîne.

width
Int32

Largeur maximale de la chaîne en pixels.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée dans le paramètre text comme dessiné avec le paramètre font.

S’applique à

MeasureString(String, Font, SizeF)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée dans la zone de disposition spécifiée.

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

Paramètres

text
String

Chaîne à mesurer.

font
Font

Font définit le format de texte de la chaîne.

layoutArea
SizeF

SizeF structure qui spécifie la zone de disposition maximale du texte.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée par le paramètre text comme dessiné avec le paramètre font.

Exceptions

font est null.

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre du gestionnaire d’événements Paint. Le code effectue les actions suivantes :

  • Crée une chaîne pour mesurer et un objet de police défini sur Arial (16 points).

  • Définit la taille de disposition maximale de la chaîne.

  • Crée un objet de taille et l’utilise, l’objet de police et la taille de disposition maximale pour mesurer la taille de la chaîne.

  • Dessine un rectangle rouge à l’aide de la taille mesurée de la chaîne.

  • Dessine la chaîne dans le rectangle dessiné.

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

Remarques

La méthode MeasureString est conçue pour être utilisée avec des chaînes individuelles et inclut une petite quantité d’espace supplémentaire avant et après la chaîne pour permettre des glyphes en surplomb. En outre, la méthode DrawString ajuste les points de glyphe pour optimiser la qualité de l’affichage et peut afficher une chaîne plus étroite que celle signalée par MeasureString. Pour obtenir des métriques adaptées aux chaînes adjacentes dans la disposition (par exemple, lors de l’implémentation du texte mis en forme), utilisez la méthode MeasureCharacterRanges ou l’une des méthodes MeasureString qui accepte un StringFormatet passez GenericTypographic. Vérifiez également que la TextRenderingHint pour la Graphics est AntiAlias.

Voir aussi

S’applique à

MeasureString(String, Font, Int32)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Fontspécifiée.

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

Paramètres

text
String

Chaîne à mesurer.

font
Font

Font qui définit le format de la chaîne.

width
Int32

Largeur maximale de la chaîne en pixels.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée dans le paramètre text comme dessiné avec le paramètre font.

Exceptions

font est null.

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre du gestionnaire d’événements Paint. Le code effectue les actions suivantes :

  • Crée une chaîne pour mesurer et un objet de police défini sur Arial (16 points).

  • Définit la largeur maximale de la chaîne.

  • Crée un objet de taille et l’utilise, l’objet de police et la largeur de chaîne maximale pour mesurer la taille de la chaîne.

  • Dessine un rectangle rouge à l’aide de la taille mesurée de la chaîne.

  • Dessine la chaîne dans le rectangle dessiné.

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

Remarques

Le paramètre width spécifie la valeur maximale du composant largeur de la structure de SizeF retournée (Width). Si le paramètre width est inférieur à la largeur réelle de la chaîne, le composant Width retourné est tronqué à une valeur représentant le nombre maximal de caractères qui correspondront à la largeur spécifiée. Pour prendre en charge l’intégralité de la chaîne, le composant Height retourné est ajusté à une valeur qui permet d’afficher la chaîne avec un wrapper de caractères.

La méthode MeasureString est conçue pour être utilisée avec des chaînes individuelles et inclut une petite quantité d’espace supplémentaire avant et après la chaîne pour permettre des glyphes en surplomb. En outre, la méthode DrawString ajuste les points de glyphe pour optimiser la qualité de l’affichage et peut afficher une chaîne plus étroite que celle signalée par MeasureString. Pour obtenir des métriques adaptées aux chaînes adjacentes dans la disposition (par exemple, lors de l’implémentation du texte mis en forme), utilisez la méthode MeasureCharacterRanges ou l’une des méthodes MeasureString qui accepte un StringFormatet passez GenericTypographic. Vérifiez également que la TextRenderingHint pour la Graphics est AntiAlias.

Voir aussi

S’applique à

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

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Paramètres

text
ReadOnlySpan<Char>

Chaîne à mesurer.

font
Font

Font définit le format de texte de la chaîne.

origin
PointF

PointF structure qui représente l’angle supérieur gauche de la chaîne.

stringFormat
StringFormat

StringFormat qui représente des informations de mise en forme, telles que l’interligne, pour la chaîne.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée par le paramètre text comme dessiné avec le paramètre font et le paramètre stringFormat.

S’applique à

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

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Paramètres

text
ReadOnlySpan<Char>

Chaîne à mesurer.

font
Font

Font qui définit le format de texte de la chaîne.

width
Int32

Largeur maximale de la chaîne.

format
StringFormat

StringFormat qui représente des informations de mise en forme, telles que l’interligne, pour la chaîne.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée dans le paramètre text comme dessiné avec le paramètre font et le paramètre stringFormat.

S’applique à

MeasureString(String, Font, PointF, StringFormat)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Paramètres

text
String

Chaîne à mesurer.

font
Font

Font définit le format de texte de la chaîne.

origin
PointF

PointF structure qui représente l’angle supérieur gauche de la chaîne.

stringFormat
StringFormat

StringFormat qui représente des informations de mise en forme, telles que l’interligne, pour la chaîne.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée par le paramètre text comme dessiné avec le paramètre font et le paramètre stringFormat.

Exceptions

font est null.

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre du gestionnaire d’événements Paint. Le code effectue les actions suivantes :

  • Crée une chaîne à mesurer et un objet de police défini sur Arial (16 points)

  • Crée un point pour localiser le coin supérieur gauche de la chaîne.

  • Crée un objet de format de chaîne et définit ses indicateurs de format sur DirectionVertical.

  • Crée un objet de taille pour mesurer la chaîne.

  • Mesure la taille de la chaîne, à l’aide de la chaîne, de l’objet de police, du point de localisation et du format de chaîne.

  • Dessine un rectangle rouge à l’aide du point de localisation et de la taille mesurée de la chaîne.

  • Dessine la chaîne dans le rectangle dessiné.

Le résultat est un rectangle vertical englobant une chaîne verticale.

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

Remarques

La méthode MeasureString est conçue pour être utilisée avec des chaînes individuelles et inclut une petite quantité d’espace supplémentaire avant et après la chaîne pour permettre des glyphes en surplomb. En outre, la méthode DrawString ajuste les points de glyphe pour optimiser la qualité de l’affichage et peut afficher une chaîne plus étroite que celle signalée par MeasureString. Pour obtenir des métriques adaptées aux chaînes adjacentes dans la disposition (par exemple, lors de l’implémentation du texte mis en forme), utilisez la méthode MeasureCharacterRanges ou l’une des méthodes MeasureString qui accepte un StringFormatet passez GenericTypographic. Vérifiez également que la TextRenderingHint pour la Graphics est AntiAlias.

Voir aussi

S’applique à

MeasureString(String, Font, SizeF, StringFormat)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Paramètres

text
String

Chaîne à mesurer.

font
Font

Font définit le format de texte de la chaîne.

layoutArea
SizeF

SizeF structure qui spécifie la zone de disposition maximale du texte.

stringFormat
StringFormat

StringFormat qui représente des informations de mise en forme, telles que l’interligne, pour la chaîne.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée dans le paramètre text comme dessiné avec le paramètre font et le paramètre stringFormat.

Exceptions

font est null.

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre du gestionnaire d’événements Paint. Le code effectue les actions suivantes :

  • Crée une chaîne pour mesurer et un objet de police défini sur Arial (16 points).

  • Définit la taille de disposition maximale de la chaîne, en créant un objet de taille pour mesurer la chaîne.

  • Crée un objet de format de chaîne et définit ses indicateurs de format sur DirectionVertical.

  • Mesure la taille de la chaîne, à l’aide de la chaîne, de l’objet police, de la taille de disposition maximale et du format de chaîne.

  • Dessine un rectangle rouge à l’aide de la taille mesurée de la chaîne.

  • Dessine la chaîne dans le rectangle dessiné.

Le résultat est un rectangle vertical englobant une chaîne verticale.

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

Remarques

La méthode MeasureString est conçue pour être utilisée avec des chaînes individuelles et inclut une petite quantité d’espace supplémentaire avant et après la chaîne pour permettre des glyphes en surplomb. En outre, la méthode DrawString ajuste les points de glyphe pour optimiser la qualité de l’affichage et peut afficher une chaîne plus étroite que celle signalée par MeasureString. Pour obtenir des métriques adaptées aux chaînes adjacentes dans la disposition (par exemple, lors de l’implémentation du texte mis en forme), utilisez la méthode MeasureCharacterRanges ou l’une des méthodes MeasureString qui accepte un StringFormatet passez GenericTypographic. Vérifiez également que la TextRenderingHint pour la Graphics est AntiAlias.

Voir aussi

S’applique à

MeasureString(String, Font, Int32, StringFormat)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Paramètres

text
String

Chaîne à mesurer.

font
Font

Font qui définit le format de texte de la chaîne.

width
Int32

Largeur maximale de la chaîne.

format
StringFormat

StringFormat qui représente des informations de mise en forme, telles que l’interligne, pour la chaîne.

Retours

Cette méthode retourne une structure SizeF qui représente la taille, dans les unités spécifiées par la propriété PageUnit, de la chaîne spécifiée dans le paramètre text comme dessiné avec le paramètre font et le paramètre stringFormat.

Exceptions

font est null.

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre du gestionnaire d’événements Paint. Le code effectue les actions suivantes :

  • Crée une chaîne pour mesurer et un objet de police défini sur Arial (16 points).

  • Définit la largeur maximale de la chaîne.

  • Crée un objet de format de chaîne et définit ses indicateurs de format sur DirectionVertical.

  • Crée un objet de taille pour mesurer la chaîne.

  • Mesure la taille de la chaîne, à l’aide de la chaîne, de l’objet de police, de la largeur maximale et du format de chaîne.

  • Dessine un rectangle rouge à l’aide de la taille mesurée de la chaîne.

  • Dessine la chaîne dans le rectangle dessiné.

Le résultat est un rectangle vertical englobant une chaîne verticale.

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

Remarques

La méthode MeasureString est conçue pour être utilisée avec des chaînes individuelles et inclut une petite quantité d’espace supplémentaire avant et après la chaîne pour permettre des glyphes en surplomb. En outre, la méthode DrawString ajuste les points de glyphe pour optimiser la qualité de l’affichage et peut afficher une chaîne plus étroite que celle signalée par MeasureString. Pour obtenir des métriques adaptées aux chaînes adjacentes dans la disposition (par exemple, lors de l’implémentation du texte mis en forme), utilisez la méthode MeasureCharacterRanges ou l’une des méthodes MeasureString qui accepte un StringFormatet passez GenericTypographic. Vérifiez également que la TextRenderingHint pour la Graphics est AntiAlias.

Voir aussi

S’applique à

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

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Paramètres

text
ReadOnlySpan<Char>

Chaîne à mesurer.

font
Font

Font qui définit le format de texte de la chaîne.

layoutArea
SizeF

SizeF structure qui spécifie la zone de disposition maximale du texte.

stringFormat
StringFormat

StringFormat qui représente des informations de mise en forme, telles que l’interligne, pour la chaîne.

charactersFitted
Int32

Nombre de caractères dans la chaîne.

linesFilled
Int32

Nombre de lignes de texte dans la chaîne.

Retours

Cette méthode retourne une structure SizeF qui représente la taille de la chaîne, dans les unités spécifiées par la propriété PageUnit, du paramètre text comme dessiné avec le paramètre font et le paramètre stringFormat.

S’applique à

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

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

Mesure la chaîne spécifiée lorsqu’elle est dessinée avec la Font spécifiée et mise en forme avec la StringFormatspécifiée.

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

Paramètres

text
String

Chaîne à mesurer.

font
Font

Font qui définit le format de texte de la chaîne.

layoutArea
SizeF

SizeF structure qui spécifie la zone de disposition maximale du texte.

stringFormat
StringFormat

StringFormat qui représente des informations de mise en forme, telles que l’interligne, pour la chaîne.

charactersFitted
Int32

Nombre de caractères dans la chaîne.

linesFilled
Int32

Nombre de lignes de texte dans la chaîne.

Retours

Cette méthode retourne une structure SizeF qui représente la taille de la chaîne, dans les unités spécifiées par la propriété PageUnit, du paramètre text comme dessiné avec le paramètre font et le paramètre stringFormat.

Exceptions

font est null.

Exemples

L’exemple de code suivant est conçu pour être utilisé avec Windows Forms et nécessite PaintEventArgse, qui est un paramètre du gestionnaire d’événements Paint. Le code effectue les actions suivantes :

  • Crée une chaîne à mesurer et un objet de police défini sur Arial (16 points)

  • Définit la taille de disposition maximale de la chaîne.

  • Crée un objet de format de chaîne et définit ses indicateurs de format sur DirectionVertical.

  • Crée les variables entières charactersFitted et linesFilled et un objet de taille pour mesurer la chaîne.

  • Mesure la taille de la chaîne et détermine le nombre de caractères ajustés et de lignes remplies, à l’aide de la chaîne, de l’objet de police, de la taille de disposition maximale et du format de chaîne.

  • Dessine un rectangle rouge à l’aide de la taille mesurée de la chaîne.

  • Dessine la chaîne dans le rectangle dessiné.

  • Dessine les valeurs du nombre de caractères ajustés et de lignes remplies.

Le résultat est un rectangle vertical englobant une chaîne verticale.

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

Remarques

La méthode MeasureString est conçue pour être utilisée avec des chaînes individuelles et inclut une petite quantité d’espace supplémentaire avant et après la chaîne pour permettre des glyphes en surplomb. En outre, la méthode DrawString ajuste les points de glyphe pour optimiser la qualité de l’affichage et peut afficher une chaîne plus étroite que celle signalée par MeasureString. Pour obtenir des métriques adaptées aux chaînes adjacentes dans la disposition (par exemple, lors de l’implémentation du texte mis en forme), utilisez la méthode MeasureCharacterRanges ou l’une des méthodes MeasureString qui accepte un StringFormat et passent GenericTypographic. Vérifiez également que la TextRenderingHint pour la Graphics est AntiAlias.

Voir aussi

S’applique à