February 27, 2021
Hot Topics:

Graphics Programming Using C# - Part 2

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

In this second of two parts, you'll discover the workings of System.Drawing.Drawing2D namespace with the help of examples. In the end, you will learn about how to work with Images by using C#.

Creating Shapes with Dashes, Dots....

You can render shapes with lines, dots, and so forth in all programming languages. You can add these functionalities easily by using Visual Basic 6.0. In .NET, you have to make use of the System.Drawing.Drawing2D namespace to achieve this functionality. This namespace provides advanced techniques for manipulating Pen and Brush objects. For example, you can change the look and feel of lines by applying the values of DashStyle Enumerator like Dash, DashDot, DashDotDot, and so on. We will discuss Brush objects later in this part.

Listing 1 examines the working of Pen objects using various DrawXXX() methods of the System.Drawing.Drawing2D namespace.

Listing 1

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

public class Drawgra:Form  {

  public Drawgra()  {
   this.Text = "Illustrating DrawXXX() methods";
   this.Size = new Size(450,400);
   this.Paint += new PaintEventHandler(Draw_Graphics);

  public void Draw_Graphics(object sender,PaintEventArgs e) {
   Graphics g = e.Graphics;
   Pen penline = new Pen(Color.Red,5);
   Pen penellipse = new Pen(Color.Blue,5);
   Pen penpie = new Pen(Color.Tomato,3);
   Pen penpolygon = new Pen(Color.Maroon,4);

   /*DashStyle Enumeration values are Dash,
     DashDot, DashDotDot, Dot, Solid, etc*/

   penline.DashStyle = DashStyle.Dash;

   //Draws an Ellipse
   penellipse.DashStyle = DashStyle.DashDotDot;

   //Draws a Pie
   penpie.DashStyle = DashStyle.Dot;

   //Draws a Polygon
   g.DrawPolygon(penpolygon,new Point[]{
   new Point(30,140),
   new Point(270,250),
   new Point(110,240),
   new Point(200,170),
   new Point(70,350),
   new Point(50,200)});

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


From the preceding code, you can see that four Pen objects are created with different colors and thicknesses. The code is reproduced here for explanation purposes:

Pen penline    = new Pen(Color.Red,5);
Pen penellipse = new Pen(Color.Blue,5);
Pen penpie     = new Pen(Color.Tomato,3);

The values of the DashStyle enumerator are applied to each of these objects as in the following code fragment:

penline.DashStyle = DashStyle.Dash;

Finally, these objects are called as an argument to various DrawXXX() methods as shown below:


Figure 1 displays the output upon execution of the preceding code. You can see from the preceding code how complex the code for rendering a polygon is. It would be easy for you if you understood the concept and logic behind the above aspects.

Figure 1  Output

Page 1 of 3

This article was originally published on July 31, 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