AddThis Social Bookmark Button

Print

System.Drawing with C#
Pages: 1, 2, 3

The Pen Class

The Pen class represents a pen that you can use for drawing a shape or writing text. A Pen object can have a color, a width, a brush, and other properties. The easiest way to construct a Pen is by passing a Color object to one of its constructors. For instance, the following code creates a yellow pen.




Pen myPen = new Pen(Color.Yellow );

A Pen object constructed using this code has a default width of 1 and a default brush. Another constructor lets you specify a width as well as a color. For example, the following Pen object is yellow with a width of 2:


Pen myPen = new Pen(Color.Yellow, 2);

The Pen class also provides some advanced properties to customize your Pen object. For example, you can create a Pen that draws a dashed line by specifying a value for the DashStyle property, as shown in the following code:


myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;

More examples of Pen objects can be found in "The Graphics Class" section later. Note also that the Pens class has static properties that allow you to obtain Pen objects of different colors.

The Brush Class

You use a Pen to draw the lines of a shape or to write text on it. To fill the interior of a shape you draw, you use a brush, which in the .NET Framework Class Library is represented by the Brush class and its five derived classes. The Brush class itself is an abstract class; therefore, it cannot be instantiated. You can, however, instantiate one of its child classes: HatchBrush, LinearGradientBrush, PathGradientBrush, SolidBrush, and TextureBrush. HatchBrush represents a hatch-styled rectangular brush; LinearGradientBrush represents a brush that paints with a linear gradient; PathGradientBrush is used to fill a GraphicPath with a gradient, SolidBrush represents a brush that fills an area evenly, and TextureBrush represents a brush with a texture image to fill an area. For example, here is the code that draws a filled rectangle on a form using a SolidBrush:


// get the Graphics of a form
Graphics g = form.CreateGraphics();
SolidBrush solidBrush = new SolidBrush(Color.Blue);
g.FillRectangle(solidBrush, 10, 20, 100, 100);

Note that the Brushes class contains static properties that allow you to get a Brush object of different colors. For instance, the previous code can be modified as follows.


Graphics g = this.CreateGraphics();
g.FillRectangle(Brushes.Blue, 10, 20, 100, 100);

See how easy it is to obtain a Brush object using the Brushes class?

The Graphics Class

Related Reading

C# Essentials
By Ben Albahari, Peter Drayton, Brad Merrill

The Graphics class represents a rectangular area on which various shapes can be drawn and text can be written. The constructor of this class is private, so there is no way you can instantiate a Graphics class using the new keyword. However, you can obtain a Graphics object that represents some other object, such as a form or a control. Once you obtain a Graphics object, you can use its various methods to draw many different shapes on it. For example, the System.Windows.Forms.Control class has the CreateGraphics method that returns a Graphics object representing that control. This is also the method that you need to call first if you want to draw on a form, as we will see later in this section. The methods of the Graphics class often require you to pass a Pen or a Brush object to create a shape. As one example, the DrawLine method of the Graphics class can be used to draw a line. One of its overloads has the following signature:


public void DrawLine ( 
  Pen pen, 
      int x1,
      int y1,
      int x2,
      int y2,
);

This overload accepts a Pen object and four integers representing the starting coordinate (x1, y1) and the end coordinate (x2, y2) of the line. For example, the following code obtains the Graphics object of a form, creates a Pen object, and passes the Pen object to the DrawLine method to draw a line on the form:


Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Yellow, 2);
// set the pen's DashStyle to dash - dot
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
g.DrawLine(pen, 0, 0, 100, 200);

The line is drawn from the coordinate (0, 0) to the coordinate (100, 200).

Graphics Class Methods

The following are some other methods of the Graphics class:

DrawLines

Draws lines that connect a series of points in an array, and is passed as the second parameter to the method. The first parameter is a Pen object. For example, the following code declares and instantiates an array of 4 points and draws lines connecting those points on a form:


Point[] points = new Point[4];
points[0] = new Point(0, 0);
points[1] = new Point(0, 120);
points[2] = new Point(20, 120);
points[3] = new Point(20, 0);
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Yellow, 2);
g.DrawLines(pen, points);

DrawRectangle

Draws a rectangle. For example, the following code draws a rectangle on a form by passing a Pen object and four integers. The first two integers represent the coordinate of the top-left corner of the rectangle, the third coordinate is the width of the rectangle, and the last integer the height of the rectangle.


Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Aquamarine, 2);
g.DrawRectangle(pen, 10, 10, 100, 20);

In this example, the top-left corner of the rectangle is at coordinate (10, 10), and it has a width of 100 pixels and a height of 20 pixels.

DrawEllipse

Draws an ellipse within a containing imaginary rectangle. For example, the following code draws an ellipse having a width of 100 pixels and a height of 20. The top-left corner of the imaginary rectangle is at coordinate (10, 10).


Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Aquamarine, 2);
g.DrawEllipse(pen, 10, 10, 100, 20);

FillRectangle

Draws a filled rectangle. For example, the following code draws a filled rectangle on a form by passing a SolidBrush object and four integers. The first two integers represent the coordinate of the top-left corner of the rectangle. The rectangle has a width of 100 pixels and a height of 30 pixels.


Graphics g = this.CreateGraphics();
SolidBrush solidBrush = new SolidBrush(Color.DarkCyan);
g.FillRectangle(solidBrush, 10, 10, 100, 30);

FillEllipse

Draws a filled ellipse. This method is similar to DrawEllipse, but the resulting ellipse is filled. Instead of passing a Pen object, the method requires a Brush object. The following code draws a filled ellipse on a form:


Graphics g = this.CreateGraphics();
SolidBrush solidBrush = new SolidBrush(Color.DarkCyan);
g.FillEllipse(solidBrush, 10, 10, 100, 30);

DrawString

Writes a string of text at a specified location on the Graphics object. For an example, see the Font class.

Save

Persists the current Graphics object into a GraphicsState object. Later, you can retrieve the persisted object using the Restore method.

Restore

Restores a previously persisted Graphics object from a System.Drawing.Drawing2D.GraphicState object. For example, the following code saves the state of a Graphics object into a System.Drawing.Drawing2D.GraphicState object and then immediately restores it.


Graphics g = this.CreateGraphics();
// save the state of the current Graphics object
System.Drawing.Drawing2D.GraphicsState graphicsState = g.Save();
// do something here
  .
  .
  .
// restore it
g.Restore(graphicsState);

Pages: 1, 2, 3

Next Pagearrow