This article has been excerpted from book "Graphics Programming with GDI+ ".
Drawing Text
This section briefly discusses the drawing of text.
The DrawString method draws a text string on a graphics surface. It has many overloaded forms. Drawstring takes arguments that identify the text, font, brush, starting location, and string format.
The simplest form of DrawString looks like this:
public void DrawString (string, Font, Brush, PointF);
Where string is the text that you want to draw, Font and Brush are the font and brushes used to draw the text, and PointF is the starting point of the text.
Listing 3.6 uses the DrawString method to draw "Hello GDI+ World!" on a form.
LISTING 3.6: Drawing text
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawString("Hello GDI+ World!",
new Font("Verdana", 16),
new SolidBrush(Color.Red),
new Point(20, 20));
}
Note: You might notice in Listing 3.6 that we create Font, SolidBrush, and Point objects directly as parameters of the DrawString method. This method of creating objects means that we can't dispose of these objects, so some cleanup is left for the garbage collector.
Figure 3.7 shows the output from Listing 3.6
The DrawString method has several overloaded forms, as shown here:
public void DrawString (string, Font, Brush, RectangleF);
public void DrawString (string, Font, Brush, PointF, StringFormat);
public void DrawString (string, Font, Brush, RectangleF, StringFormat);
public void DrawString (string, Font, Brush, float, float);
public void DrawString (string, Font, Brush, float, float, String Format);
![fig3.7.gif](https://www.c-sharpcorner.com/UploadFile/mahesh/drawing-text-in-gdi/Images/fig3.7.gif)
FIGURE 3.7: Drawing text
Now let's see another example of drawing text- this time using the StringFormat class, which defines the text format. Using StringFormat, you can set flags, alignment, trimming, and other options for the text. Listing 3.7 shows different ways to draw text on a graphics surface. In this example the FormatFlags property is set to StringFormatFlags.DirectionVertical, which draws a vertical text.
LISTING 3.7: Using DrawString to draw text on a graphics surface
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
// create brushes
SolidBrush blueBrush = new SolidBrush(Color.Blue);
SolidBrush redBrush = new SolidBrush(Color.Red);
SolidBrush greenBrush = new SolidBrush(Color.Green);
// Create a rectangle
Rectangle rect = new Rectangle(20, 20, 200, 100);
// The text to be drawn
String DrawString = "Hello GDI+ World!";
// Create a Font object
Font DrawFont = new Font("Verdana", 14);
float x = 100.0F;
float y = 100.0F;
StringFormat DrawFormat = new StringFormat();
// Set string format flag to direction vertical,
// which draws text vertically
DrawFormat.FormatFlags =
StringFormatFlags.DirectionVertical;
// Draw string
e.Graphics.DrawString("Drawing text",
new Font("Tahoma", 14), greenBrush, rect);
e.Graphics.DrawString(DrawString, new Font("Arial", 12),
redBrush, 120, 140);
e.Graphics.DrawString(DrawString, DrawFont,
blueBrush, x, y, DrawFormat);
// Dispose of objects
blueBrush.Dispose();
redBrush.Dispose();
greenBrush.Dispose();
DrawFont.Dispose();
}
Figure 3.8 shows the output from Listing 3.7
![fig3.8.gif](https://www.c-sharpcorner.com/UploadFile/mahesh/drawing-text-in-gdi/Images/fig3.8.gif)
FIGURE 3.8: Drawing text with different directions
Conclusion
Hope the article would have helped you in understanding drawing Text in GDI+. Read other articles on GDI+ on the website.
![bookGDI.jpg](https://www.c-sharpcorner.com/UploadFile/mahesh/drawing-text-in-gdi/Images/bookGDI.jpg) |
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows. |