Procedimiento para dibujar texto ajustado en un rectángulo

Puede dibujar texto encapsulado en un rectángulo mediante el método sobrecargado DrawString de la clase Graphics que toma un parámetro Rectangle o RectangleF. Además, usará un objeto Brush y Font.

También puede dibujar texto encapsulado en un rectángulo mediante el método sobrecargado DrawText de TextRenderer que toma un parámetro Rectangle o TextFormatFlags. Además, usará un objeto Color y Font.

En la ilustración siguiente se muestra la salida del texto dibujado en el rectángulo cuando se usa el método DrawString:

Captura de pantalla que muestra la salida al usar el método DrawString.

Para dibujar texto encapsulado en un rectángulo con GDI+

  1. Use el método sobrecargado DrawString y pase el texto que quiera: Rectangle o RectangleF, Font y Brush.

    string text1 = "Draw text in a rectangle by passing a RectF to the DrawString method.";
    using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1));
    }
    
    Dim text1 As String = "Draw text in a rectangle by passing a RectF to the DrawString method."
    Dim font1 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rectF1 As New RectangleF(30, 10, 100, 122)
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1)
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1))
    Finally
        font1.Dispose()
    End Try
    

Para dibujar texto encapsulado en un rectángulo con GDI

  1. Use el valor de enumeración TextFormatFlags para especificar que el texto debe encapsularse con el método sobrecargado DrawText y pase el texto que quiera: Rectangle, Font y Color.

    string text2 = "Draw text in a rectangle by passing a RectF to the DrawString method.";
    using (Font font2 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        Rectangle rect2 = new Rectangle(30, 10, 100, 122);
    
        // Specify the text is wrapped.
        TextFormatFlags flags = TextFormatFlags.WordBreak;
        TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags);
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rect2));
    }
    
    Dim text2 As String = _
        "Draw text in a rectangle by passing a RectF to the DrawString method."
    Dim font2 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rect2 As New Rectangle(30, 10, 100, 122)
        
        ' Specify the text is wrapped.
        Dim flags As TextFormatFlags = TextFormatFlags.WordBreak
        TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags)
        e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rect2))
    Finally
        font2.Dispose()
    End Try
    

Compilar el código

Los ejemplos anteriores requieren:

Consulte también