Contents

Theory about applications - [TA5]

Question
Give a simple introduction to graphics in the .NET environment. How to create a bitmap and a chart on it.

Introduction

The Graphics class encapsulates GDI+ drawing surfaces. Before drawing any object (for example circle, or rectangle) we have to create a surface using Graphics class. Generally we use Paint event of a Form to get the reference of the graphics. Another way is to override OnPaint method. Here is how you get a reference of the Graphics object:

private void form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
}
//OR
protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;
}

Once you have the Graphics reference, you can call any of this class’s members to draw various objects. Here are some of Graphics class’s methods:

Method Description
DrawArc Draws an arc from the specified ellipse.
DrawClosedCurve Draws a closed curve defined by an array of points.
DrawCurve Draws a curve defined by an array of points.
DrawImage Draws an image.
DrawLine Draws a line.
DrawRectangle Draws the outline of a rectangle.
FillRectangle Fills the interior of a rectangle with a Brush.

Graphics Objects

After creating a Graphics object, you can use it draw lines, fill shapes, draw text and so on. The major objects are:

Object Description
Brush Used to fill enclosed surfaces with patterns,colors, or bitmaps.
Pen Used to draw lines and polygons, including rectangles, arcs, and pies
Font Used to describe the font to be used to render text
Color Used to describe the color used to render a particular object. In GDI+ color can be alpha blended

The Pen Class

A pen draws a line of specified width and style. You always use Pen constructor to create a pen. The constructor initializes a new instance of the Pen class. You can initialize it with a color or brush.
Initializes a new instance of the Pen class with the specified Color and Width.

public Pen(Color, float);
//Here is one example:
Pen pn = new Pen( Color.Blue, 100 );

The Brush Class

The Brush class is an abstract base class and cannot be instantiated. We always use its derived classes to instantiate a brush object, such as SolidBrush, TextureBrush, RectangleGradientBrush, and LinearGradientBrush.
Here is one example:

Brush brsh = new SolidBrush(Color.Red), 40, 40, 140, 140);

The SolidBrush class defines a brush made up of a single color. Brushes are used to fill graphics shapes such as rectangles, ellipses, pies, polygons, and paths.
The TextureBrush encapsulates a Brush that uses an fills the interior of a shape with an image.
The LinearGradiantBrush encapsulates both two-color gradients and custom multi-color gradients.

The Rectangle Object

Its constructor initializes a new instance of the Rectangle class. Here is the definition:

public Rectangle(Point, Size);
//OR
public Rectangle(int, int, int, int);

The Rectangle Object is used to draw a rectangle on WinForms. Besides its constructor, the Rectangle Object has following members:

Member Description
Bottom Gets the y-coordinate of the lower-right corner of the rectangular region defined by this Rectangle.
Height Gets or sets the width of the rectangular region defined by this Rectangle.
IsEmpty Tests whether this Rectangle has a Width or a Height of 0.
Left Gets the x-coordinate of the upper-left corner of the rectangular region defined by this Rectangle.
Location Gets or sets the coordinates of the upper-left corner of the rectangular region represented by this
Right Gets the x-coordinate of the lower-right corner of the rectangular region defined by this Rectangle.
Size Gets or sets the size of this Rectangle.
Top Gets the y-coordinate of the upper-left corner of the rectangular region defined by this Rectangle.
Width Gets or sets the width of the rectangular region defined by this Rectangle.
X Gets or sets the x-coordinate of the upper-left corner of the rectangular region defined by this Rectangle.
Y Gets or sets the y-coordinate of the upper-left corner of the rectangular region defined by this Rectangle.

Example: Drawing a rectangle

We can override OnPaint event of your form to draw an rectangle. The LinearGradientBrush encapsulates a brush and linear gradient.

protected override void OnPaint(PaintEventArgs pe)
{
    Graphics g = pe.Graphics ;
    Rectangle rect = new Rectangle(50, 30, 100, 100);
    LinearGradientBrush lBrush = new LinearGradientBrush(
                                    rect,
                                    Color.Red,
                                    Color.Yellow,
                                    LinearGradientMode.BackwardDiagonal
                                );
    g.FillRectangle(lBrush, rect);
}

A useful application of the rectangle object can be its use as a “canvas” for a bitmap object. Doing so will allow to control the size of the chart and its scale by interacting with the rectangle object and in particular it can be used to plot charts within its area.

Example: Creating a bitmap

Let’s now see how to create a simple bitmap that we can then use as described above, and as it can be seen in the applications developed.

Bitmap image = new Bitmap(width, height);
// We can also assign the bitmap to a picturebox to draw on it
pictureBox.Image = image;

Sources