Drawing Strings via the Graphics Object in C#

Introduction:

This tutorial is on how to draw text in .NET (C#/Visual Basic).

Labels, Textboxes?

I'm sure if you've not looked in to the 'graphics' (etc) objects in the .NET framework, you're thinking, why don't we just use labels or textboxes with their read-only property enabled. Labels and textboxes both have a surrounding area around the text which are both of different colours (however we could change this to the form background colour to give the effect of the backgrounds being removed), but would we create extra controls (labels, and textboxes) when we don't need to? That's consuming more resources than needed.

Paint Events:

We are going to be using Form1's 'paint' event to draw the text to the screen. A paint event is a script which handles the drawing of the object, all controls in the .NET framework would have some form of 'paint' event otherwise they would not be drawn to the screen and updated every frame. So in essence, the paint event handles the drawing of it's parent (the control the event is linked with) each frame in order to show updates such as additional text, button click highlights, etc.

Steps:

First create a new C#/Visual Basic project. I'll be using C# for this tutorial, then select 'Windows Form Project', give it a name and click 'Create'. Now double click on form1, click inside of the sub, then on the event panel drop down list where it currently says 'load', select 'paint'. A new 'Form1_paint' event will be created. In there, do the following...
  1. protected override void OnPaint(PaintEventArgs e)
  2. {
  3. }
We will be using the 'Graphics'.'DrawString' namespace/function to draw the string to our screen, which takes the following arguments; String text to draw, Font to draw text in (with size), Brush colour, Position, [Optional] String Format So first we want to create the string we are going to draw, I will draw 'Yorkiebar'...
  1. string text = "Yorkiebar";
Next we want to create a font to draw the text in. I will use 'Tahoma' font with the font size of '11'...
  1. Font font = new Font("Tahoma", 11);
Now for the font colour in the form a brush, I will use 'Black'...
  1. Brush brush = Brushes.Black;
For the position of the text, I will go for 100, 100 from the top left (x, y)...
  1. Point point = new Point(100, 100);
Finally we will use the 'DrawString' function to write the string using the above objects. We will leave the 'StringFormat' to default...
  1. e.Graphics.DrawString(text, font, brush, point);
Notice, we're typing this inside of our 'paint' event, where the 'e' object is one of the parameters; the 'PaintEventArgs' - as such, it already contains the needed graphics to draw the above string.

Finished!

Here is the full source...
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace GraphicsDrawing_Form
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. protected override void OnPaint(PaintEventArgs e)
  21. {
  22. string text = "Yorkiebar";
  23. Font font = new Font("Tahoma", 11);
  24. Brush brush = Brushes.Black;
  25. Point point = new Point(100, 100);
  26. e.Graphics.DrawString(text, font, brush, point);
  27. }
  28. }
  29. }

Add new comment