Free C# Tutorials

home
Stay at Home and Learn

Graphics in Visual C# .NET

 
Computer Tutorials List

 

 

 

 

C# .NET allows you to draw straight to a form or form objects. You do all the drawing with inbuilt Graphic objects.

The first thing to do is to set up a Graphics object. You then specify what it is you want to draw on. To draw directly to a form, you can do this:

Graphics surface;
surface = this.CreateGraphics();

The first line sets up a Graphics variable that we've called surface. To set the current form as the graphics object that will be drawn to, use the keyword this followed by CreateGraphics(). If you wanted to draw on, say, a label instead, the code would be this:

surface = this.label_Name.CreateGraphics();

Once you have told C# which object you want to draw on you can then use a series of inbuilt tools to do the actual drawing. Let's draw a line.

C# uses Pens to draw shapes. To set up a new pen object, you need the colour the pen will use, followed by how wide you want the lines:

Pen pen1 = new Pen( Color.Blue, 1.0f );

Here, we've set up a new pen object called pen1. In between the round brackets of Pen, we have "Color.Blue". Color is an inbuilt class. After you type the dot, you should see the IntelliSense box appear with a list of colours. Simply select the one you want. After a comma, type the width of the pen. Our width was 1.0. We've added the "f" because you need a floating point number.

Once you have a pen tool, you can use Draw methods to draw to your object. In the code below, we're using the DrawLine method:

surface.DrawLine( pen1, 10, 10, 100, 100 );

We're using the DrawLine method to draw on our surface object. In between the round brackets of DrawLine, you first need the name of your Pen. The numbers are coordinates. The first two (10, 10) mean 10 units from the left and 10 units down. This will be the starting point of the line. The second two (100, 100) are the end point of the line. So 100 units from the left and 100 units down.

To test it out, create a new project. Click your form to select it. Now have a look at the properties box on your right. Click the lightning bolt to see a list of events that the form has. Locate the Click event. Double click the word Click to bring up the code for that event. Now type the following code:

C# code to draw a line

Run your programme and click the form. You should find that a diagonal line is drawn, starting from the top left.

However, there is a problem with drawing straight to a form or form object. If you minimize the form and then maximize it, you'll find that all of the things you drew are erased. To prevent this, you can use the form Paint event. Let's see how.

Stop your programme and return to Design view. Click the form to select it. In the properties box on the right, click the lighting bolt to see the list again. Locate Paint, and then double click it (the word Paint). This will bring up the code stub for the form Paint event. Move your code from the Click event to the Paint event:

The C# Paint Event

Run your programme to see the diagonal line again. When you Minimize then Maximize the form, this time the line is not erased.

In the next part, you'll learn about the Rectangle Class.

<-- Dates and Times | Drawing Rectangles in C# .NET -->

<--Back to the C# .NET Contents Page

View all our Home Study Computer Courses