In this tutorial, we will go over how to code and draw graphics using C# inside of a Windows Application form. In some cases, you may need to some kind of visual presentation. As some graphics editing programs can be nice to help generate GIF objects within a form, sometimes you may want crisp and clean graphics that are generated by the computer for better presentation. We will go over and demonstrate three examples of drawing graphics within your form.

Setting Up Your Project

Before we can start drawing, we need to set up our project with all of the necessary files. If you have Visual Studios you can simply open the application and Start A New Project. Select C# in the left hand projects panel and then choose Windows Forms Application. Click OK and Visual Studios will compile all of the necessary files you need to code and test your project. We will be using a Windows Form as our canvas and placing a button that will command the computer to draw out the graphic for us.

Once the Project is setup, We will need to add a button to our Form1.cs document. In the design view of this document you will see a pre-made form. Open up your Toolbox Panel to drag and drop a Button into the form area. Now that the button has been created, we can modify it’s function by double clicking it to open the code view of the button.

Draw a Line

In our first example, we will be drawing a simple line and break down the code. If you’d like, you can change the button text to inform the user of it’s actions. Simply open up the Properties Panel and look for the line Table Row titled Text. Here is where you can change the Buttons text to whatever you like. Go back to your button’s code view to insert the code below.

When looking at the coding for the button, you will see that you can place your code within the private void condition. Going through the code you will see that it is separated basically into three parts: creating some variables, Create a pen to draw the object and then place the coordinates to draw the graphic. We go through a process of notifying we are using the Drawing Graphics method, specifying the characteristics of the pen and then plotting the graphics with plotting points. The syntax for placing coordinates follows a x1, y1, x2, y2 grouping. Press the Start button to compile and run the code and test out your form. It should look similar to the image below.

Draw a Rectangle

Next we will insert some code that will allow us to draw a rectangle instead of a solid line. Following the same process, Double-Click on the button tool, then copy and paste the below code into the Buttons Control file.

Our setup in this example is very similar to the previous one from before. This time, we made some slight modifications in order to change the results. The myPen variable now uses a Drawing object which will help us “Draw” the shape. We then must create a variable containing the information for the rectangle which we will call myRectangle. It is still necessary to state the coordinates of the rectangle by following the parameter syntax (myPen, X, Y, width, height). Press the Start button to compile and run the code and test out your form. It should look similar to the image below.

Draw a Dotted Rectangle

Our last example, we will create another rectangle, but this time we will have a dotted line stroke around it instead of the solid line. The code is very similar to the previous example but we must create an extra object and combine it with the myPen variable.

By creating a dashValues and attaching it to myPen, we are able to alter the pen’s original method of drawing a line. When using C#, we must specifiy values for the dashed lines which allows us to greatly control the look of the rectangle.

Wrapping it Up

Using the computer to help draw your graphics can give your clean and computer aided designs with technical precision. This may also be a good tool to utilize if you do not have access to a graphics editor or may not be familiar on how to use one. Try changing the values of these examples and get results that may help expand your coding knowledge and library.