Edit

Share via


How to: Draw Text at a Specified Location

When you perform custom drawing, you can draw text in a single horizontal line starting at a specified point. You can draw text in this manner by using the DrawString overloaded method of the Graphics class that takes a Point or PointF parameter. The DrawString method also requires a Brush and Font

You can also use the DrawText overloaded method of the TextRenderer that takes a Point. DrawText also requires a Color and a Font.

The following illustration shows the output of text drawn at a specified point when you use the DrawString overloaded method.

Screenshot that shows the output of text at a specified point.

To draw a line of text with GDI+

  1. Use the DrawString method, passing the text you want, Point or PointF, Font, and Brush.

    using (Font font1 = new Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel)){
    PointF pointF1 = new PointF(30, 10);
    e.Graphics.DrawString("Hello", font1, Brushes.Blue, pointF1);
    }
    
    Dim font1 As New Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel)
    Try
        Dim pointF1 As New PointF(30, 10)
        e.Graphics.DrawString("Hello", font1, Brushes.Blue, pointF1)
    Finally
        font1.Dispose()
    End Try
    

To draw a line of text with GDI

  1. Use the DrawText method, passing the text you want, Point, Font, and Color.

    using (Font font = new Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel))
    {
        Point point1 = new Point(30, 10);
        TextRenderer.DrawText(e.Graphics, "Hello", font, point1, Color.Blue);
    }
    
    Dim font As New Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel)
    Try
        Dim point1 As New Point(30, 10)
        TextRenderer.DrawText(e.Graphics, "Hello", font, point1, Color.Blue)
    Finally
        font.Dispose()
    End Try
    

Compiling the Code

The previous examples require:

See also