TextRenderer.MeasureText Método

Definición

Mide el texto especificado cuando se traza con la fuente que se concreta.

Sobrecargas

MeasureText(String, Font, Size, TextFormatFlags)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente e instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

MeasureText(ReadOnlySpan<Char>, Font, Size, TextFormatFlags)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente e instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

MeasureText(IDeviceContext, String, Font, Size)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada en el contexto de dispositivo dado y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font, Size)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada en el contexto de dispositivo dado y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

MeasureText(String, Font, Size)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font)

Proporciona el tamaño, en píxeles, del texto especificado trazado con la fuente indicada en el contexto de dispositivo que se concrete.

MeasureText(IDeviceContext, String, Font)

Proporciona el tamaño, en píxeles, del texto especificado trazado con la fuente indicada en el contexto de dispositivo que se concrete.

MeasureText(String, Font)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente determinada.

MeasureText(ReadOnlySpan<Char>, Font)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente determinada.

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font, Size, TextFormatFlags)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con el dispositivo de contexto, la fuente y las instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

MeasureText(ReadOnlySpan<Char>, Font, Size)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con el dispositivo de contexto, la fuente y las instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

MeasureText(String, Font, Size, TextFormatFlags)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente e instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

public:
 static System::Drawing::Size MeasureText(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize, System::Windows::Forms::TextFormatFlags flags);
public static System.Drawing.Size MeasureText (string text, System.Drawing.Font font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
public static System.Drawing.Size MeasureText (string? text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
static member MeasureText : string * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size
Public Shared Function MeasureText (text As String, font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

Parámetros

text
String

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

proposedSize
Size

Size del rectángulo delimitador inicial.

flags
TextFormatFlags

Instrucciones de formato que se van a aplicar al texto medido.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font y el formato especificados.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar uno de los MeasureText métodos . Para ejecutar este ejemplo, pegue el código en un formulario Windows Forms y llame DrawALineOfText desde el controlador de eventos del Paint formulario, pasando e como PaintEventArgs.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

Comentarios

MeasureText usa los proposedSize parámetros y flags para indicar la relación de alto al ancho al determinar el tamaño del texto. Al medir el texto en una sola línea, si el proposedSize parámetro representa un Size con una dimensión de alto mayor que Int32.MaxValue, el devuelto Size se ajustará para reflejar el alto real del texto.

Puede manipular cómo se traza el texto mediante una de las sobrecargas de DrawText que toma un parámetro TextFormatFlags. Por ejemplo, el comportamiento predeterminado de TextRenderer es agregar relleno al rectángulo de delimitación del texto trazado para dar cabida a glifos que sobresalgan. Si tiene que trazar una línea de texto sin estos espacios extra, debe usar las versiones de DrawText y MeasureText que toman un parámetro Size y TextFormatFlags. Para obtener un ejemplo, consulte MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

Nota

Esta sobrecarga de MeasureText(String, Font, Size, TextFormatFlags) omitirá un TextFormatFlags valor de NoPadding o LeftAndRightPadding. Si especifica un valor de relleno distinto del predeterminado, debe usar la sobrecarga de MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags) que toma un IDeviceContext objeto .

Se aplica a

MeasureText(ReadOnlySpan<Char>, Font, Size, TextFormatFlags)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente e instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

public:
 static System::Drawing::Size MeasureText(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize, System::Windows::Forms::TextFormatFlags flags);
public static System.Drawing.Size MeasureText (ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
static member MeasureText : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size
Public Shared Function MeasureText (text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

Parámetros

text
ReadOnlySpan<Char>

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

proposedSize
Size

Size del rectángulo delimitador inicial.

flags
TextFormatFlags

Instrucciones de formato que se van a aplicar al texto medido.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font y el formato especificados.

Excepciones

Se aplica a

MeasureText(IDeviceContext, String, Font, Size)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada en el contexto de dispositivo dado y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Size proposedSize);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Size proposedSize);
static member MeasureText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As String, font As Font, proposedSize As Size) As Size

Parámetros

dc
IDeviceContext

Contexto de dispositivo en el que se mide el texto.

text
String

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

proposedSize
Size

Size del rectángulo delimitador inicial.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font especificado.

Excepciones

dc es null.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar uno de los MeasureText métodos . Para ejecutar este ejemplo, pegue el código en un formulario Windows Forms y llame DrawALineOfText desde el controlador de eventos del Paint formulario, pasando e como PaintEventArgs.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

Comentarios

El MeasureText método usa el proposedSize parámetro para indicar la relación de alto al ancho al determinar el tamaño del texto. Al medir el texto en una sola línea, si el proposedSize parámetro representa un Size con una dimensión de alto mayor que Int32.MaxValue, el devuelto Size se ajustará para reflejar el alto real del texto.

Se aplica a

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font, Size)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada en el contexto de dispositivo dado y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize);
static member MeasureText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size) As Size

Parámetros

dc
IDeviceContext

Contexto de dispositivo en el que se mide el texto.

text
ReadOnlySpan<Char>

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

proposedSize
Size

Size del rectángulo delimitador inicial.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font especificado.

Excepciones

El valor de dc es null.

Se aplica a

MeasureText(String, Font, Size)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

public:
 static System::Drawing::Size MeasureText(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize);
public static System.Drawing.Size MeasureText (string text, System.Drawing.Font font, System.Drawing.Size proposedSize);
public static System.Drawing.Size MeasureText (string? text, System.Drawing.Font? font, System.Drawing.Size proposedSize);
static member MeasureText : string * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size
Public Shared Function MeasureText (text As String, font As Font, proposedSize As Size) As Size

Parámetros

text
String

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

proposedSize
Size

Size del rectángulo delimitador inicial.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font especificado.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar uno de los MeasureText métodos . Para ejecutar este ejemplo, pegue el código en un formulario Windows Forms y llame DrawALineOfText desde el controlador de eventos del Paint formulario, pasando e como PaintEventArgs.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

Comentarios

El MeasureText método usa el proposedSize parámetro para indicar la relación de alto al ancho al determinar el tamaño del texto. Al medir el texto en una sola línea, si el proposedSize parámetro representa un Size con una dimensión de alto mayor que Int32.MaxValue, el devuelto Size se ajustará para reflejar el alto real del texto.

Se aplica a

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font)

Proporciona el tamaño, en píxeles, del texto especificado trazado con la fuente indicada en el contexto de dispositivo que se concrete.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font);
static member MeasureText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font) As Size

Parámetros

dc
IDeviceContext

Contexto de dispositivo en el que se mide el texto.

text
ReadOnlySpan<Char>

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font concretado en el contexto de dispositivo especificado.

Excepciones

El valor de dc es null.

Se aplica a

MeasureText(IDeviceContext, String, Font)

Proporciona el tamaño, en píxeles, del texto especificado trazado con la fuente indicada en el contexto de dispositivo que se concrete.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font);
static member MeasureText : System.Drawing.IDeviceContext * string * System.Drawing.Font -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As String, font As Font) As Size

Parámetros

dc
IDeviceContext

Contexto de dispositivo en el que se mide el texto.

text
String

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

Devoluciones

Size, en píxeles, del parámetro text trazado en una sola línea con el parámetro font concretado en el contexto de dispositivo especificado.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar uno de los MeasureText métodos . Para ejecutar este ejemplo, pegue el código en un formulario Windows Forms y llame DrawALineOfText desde el controlador de eventos del Paint formulario, pasando e como PaintEventArgs.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

Comentarios

El MeasureText método requiere que el texto se dibuje en una sola línea.

Se aplica a

MeasureText(String, Font)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente determinada.

public:
 static System::Drawing::Size MeasureText(System::String ^ text, System::Drawing::Font ^ font);
public static System.Drawing.Size MeasureText (string text, System.Drawing.Font font);
public static System.Drawing.Size MeasureText (string? text, System.Drawing.Font? font);
static member MeasureText : string * System.Drawing.Font -> System.Drawing.Size
Public Shared Function MeasureText (text As String, font As Font) As Size

Parámetros

text
String

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

Devoluciones

Size, en píxeles, del parámetro text trazado en una sola línea con el parámetro font especificado. Puede manipular cómo se traza el texto mediante una de las sobrecargas de DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags) que toma un parámetro TextFormatFlags. Por ejemplo, el comportamiento predeterminado de TextRenderer es agregar relleno al rectángulo de delimitación del texto trazado para dar cabida a glifos que sobresalgan. Si tiene que trazar una línea de texto sin estos espacios extra, debe usar las versiones de DrawText(IDeviceContext, String, Font, Point, Color) y MeasureText(IDeviceContext, String, Font) que toman un parámetro Size y TextFormatFlags. Para obtener un ejemplo, consulte MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

Ejemplos

En el ejemplo de código siguiente se muestra cómo utilizar el método MeasureText. Para ejecutar este ejemplo, pegue el código en un formulario Windows Forms y llame MeasureText1 desde el controlador de eventos del Paint formulario, pasando e como PaintEventArgs.

private void MeasureText1(PaintEventArgs e)
{
    String text1 = "Measure this text";
    Font arialBold = new Font("Arial", 12.0F);
    Size textSize = TextRenderer.MeasureText(text1, arialBold);
    TextRenderer.DrawText(e.Graphics, text1, arialBold, 
        new Rectangle(new Point(10, 10), textSize), Color.Red);  
}
Private Sub MeasureText1(ByVal e As PaintEventArgs)
    Dim text1 As String = "Measure this text"
    Dim arialBold As New Font("Arial", 12.0F)
    Dim textSize As Size = TextRenderer.MeasureText(text1, arialBold)
    TextRenderer.DrawText(e.Graphics, text1, arialBold, _
        New Rectangle(New Point(10, 10), textSize), Color.Red)

End Sub

Comentarios

El MeasureText método requiere que el texto se dibuje en una sola línea.

Se aplica a

MeasureText(ReadOnlySpan<Char>, Font)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente determinada.

public:
 static System::Drawing::Size MeasureText(ReadOnlySpan<char> text, System::Drawing::Font ^ font);
public static System.Drawing.Size MeasureText (ReadOnlySpan<char> text, System.Drawing.Font? font);
static member MeasureText : ReadOnlySpan<char> * System.Drawing.Font -> System.Drawing.Size
Public Shared Function MeasureText (text As ReadOnlySpan(Of Char), font As Font) As Size

Parámetros

text
ReadOnlySpan<Char>

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

Devoluciones

Size, en píxeles, del texto trazado en una sola línea con la fuente especificada. Puede manipular cómo se traza el texto mediante una de las sobrecargas de DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags) que toma un parámetro TextFormatFlags. Por ejemplo, el comportamiento predeterminado de TextRenderer es agregar relleno al rectángulo de delimitación del texto trazado para dar cabida a glifos que sobresalgan. Si tiene que trazar una línea de texto sin estos espacios extra, debe usar las versiones de DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color) y MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font) que toman un parámetro Size y TextFormatFlags. Para obtener un ejemplo, consulte MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

Se aplica a

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font, Size, TextFormatFlags)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con el dispositivo de contexto, la fuente y las instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize, System::Windows::Forms::TextFormatFlags flags);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
static member MeasureText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

Parámetros

dc
IDeviceContext

Contexto de dispositivo en el que se mide el texto.

text
ReadOnlySpan<Char>

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

proposedSize
Size

Size del rectángulo delimitador inicial.

flags
TextFormatFlags

Instrucciones de formato que se van a aplicar al texto medido.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font y el formato especificados.

Excepciones

dc es null.

Se aplica a

MeasureText(ReadOnlySpan<Char>, Font, Size)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

public:
 static System::Drawing::Size MeasureText(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize);
public static System.Drawing.Size MeasureText (ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize);
static member MeasureText : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size
Public Shared Function MeasureText (text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size) As Size

Parámetros

text
ReadOnlySpan<Char>

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

proposedSize
Size

Size del rectángulo delimitador inicial.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font especificado.

Se aplica a

MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con el dispositivo de contexto, la fuente y las instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize, System::Windows::Forms::TextFormatFlags flags);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string? text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
static member MeasureText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As String, font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

Parámetros

dc
IDeviceContext

Contexto de dispositivo en el que se mide el texto.

text
String

Texto que se va a medir.

font
Font

Font que se va a aplicar al texto medido.

proposedSize
Size

Size del rectángulo delimitador inicial.

flags
TextFormatFlags

Instrucciones de formato que se van a aplicar al texto medido.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font y el formato especificados.

Excepciones

dc es null.

Ejemplos

En el ejemplo siguiente se muestra cómo usar los MeasureText métodos y DrawText para dibujar una sola línea de texto en diferentes estilos de fuente. Para ejecutar este ejemplo, pegue el código siguiente en un formulario Windows Forms y llame DrawALineOfText desde el controlador de eventos del Paint formulario, pasando e como PaintEventArgs.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

Comentarios

El MeasureText método usa los proposedSize parámetros y flags para indicar la relación de alto al ancho al determinar el tamaño del texto. Al medir el texto en una sola línea, si el proposedSize parámetro representa un Size con una dimensión de alto mayor que Int32.MaxValue, el devuelto Size se ajustará para reflejar el alto real del texto.

Puede manipular cómo se traza el texto mediante una de las sobrecargas de DrawText que toma un parámetro TextFormatFlags. Por ejemplo, el comportamiento predeterminado de TextRenderer es agregar relleno al rectángulo de delimitación del texto trazado para dar cabida a glifos que sobresalgan. Si necesita dibujar una línea de texto sin estos espacios adicionales, use las versiones de DrawText y MeasureText que toman un Size parámetro y TextFormatFlags , como se muestra en el ejemplo.

Se aplica a