March 9, 2021
Hot Topics:

Graphics Programming Using C#

  • By Anand Narayanaswamy
  • Send Email »
  • More Articles »

Graphics plays an important part in every application you develop. For instance, if you are developing a paint application or code-editor software, you will want to add color and font dialog box functionality. Sometimes, you may have to print text in the form areas of the software. There may be cases where you have to decorate your text or place it within a rectangle or inside a circle. You may want to simply insert a picture as the background. The possibilities are endless. This customizations are only possible with the careful and judicious use of the appropriate concepts. This article, which is the first of two parts, explains the concepts as laid out in the Microsoft .NET Framework using C# programming language.

I assume that you have a prior knowledge of C# language, particularly in the area of WinForms. You can locate my two-part article on WinForms from .NET section of this site. Moreover, Windows 2000 is required for testing the codes described in this article along with .NET Framework SDK or Visual Studio .NET.

C# provides us with a rich set of namespaces, classes, methods and events for developing applications with graphical capabilities. With the help of its Graphics class, the System.Drawing namespace provides functionality for drawing shapes, printing texts on to the form, and much more.

The EventHandler involved here is PaintEventHandler and the corresponding event is called Paint. You have to apply the concept of event handling in order to render graphics in your C# program. Listing 1 shown below examines how to print "Welcome C#" on a WinForm. In this example,text is printed directly onto the form.

Listing 1:

using System;
using System.Windows.Forms;
using System.Drawing;

public class Hello:Form  {
  public Hello()   {
    this.Paint += new PaintEventHandler(f1_paint);

  private void f1_paint(object sender,PaintEventArgs e) {

    // Get Graphics Object
    Graphics g = e.Graphics; 

    // Method under System.Drawing.Graphics
    g.DrawString("Welcome C#",new Font("Verdana",20),
    new SolidBrush(Color.Tomato),40,40);

  public static void Main() {
    Application.Run(new Hello());


In Listing 1 the method DrawString() takes four arguments. The first argument signifies the text to be printed followed by the font name, size, and color. The next two arguments imply the painting location (X coordinate and Y coordinates) on the form.

Keep in mind that every method in Graphics class has to be accessed by creating an object of that class. You can easily update the above program to render other graphical shapes like rectangle and ellipse. All you have to do is to apply the relevant methods. The next session discusses these methods in detail.

Page 1 of 3

This article was originally published on July 30, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date