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.
To draw a line of text with GDI+
Use the DrawString method, passing the text you want, Point or PointF, Font, and Brush.
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
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); }
To draw a line of text with GDI
Use the DrawText method, passing the text you want, Point, Font, and Color.
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
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); }
Compiling the Code
The previous examples require:
- PaintEventArgs e, which is a parameter of PaintEventHandler.
See Also
Tasks
How to: Draw Text with GDI
How to: Construct Font Families and Fonts
How to: Draw Wrapped Text in a Rectangle