Friday, May 8, 2009

C# - System.Drawing namespace & Graphics Object

To have a graphic UI, .NET prepared many graphic classes to display graphical objects
on a form or other Windows Forms control.

The System.Drawing namespace is included in .NET Framework to deal with graphic stuff.
The System.Drawing namespace contains many classes and the most common ones are as following:
- [Graphics] is used to draw to display devices
- [Pen] is used to draw lines and curves
- [Brush] and other classes drived from it are used to fill shapes.
- ...

The System.Drawing namespace contains some structures too, and most important ones are as following:
- [Color] which represent a color
- [Point] that has x and y cordinates for a point in two dimensional plane.
- [Rectangle] it has four integer values that represent the location and size of
a rectangle.
- [Size] Stores width and height of a rectabgle by integer values.
- ...

How to draw a line or shape:
- Create a [Graphics] object, using System.Windows.Forms.Control.CreateGraphics method.
- Create a [Pen] object.
- Call a method from the Graphics object to draw a shape using the [Pen].

Some of the Graphics object methods:
- [Clear] Fills the entire drawing surface with a color.
- [DrawEllipse] Draws an ellipse or a circle by a pair of height and width.
- [DrawIcon] Draws image of an icon at specified coordinates.
- [DrawImage] Draws an image at specified coordinates.
- [DrawLine] Draws a line between two points.
- [DrawLines] Draws a series of lines by an array of Point structures.
- [DrawPath] Draws a series of connected lines and curves.
- [DrawPie] Draws a pie shape, it needs a upper-left coordinate pair, a width, a height, and two radial lines.
- [DrawPolygon] Draws a multi sides shape by an array of Point structures.
- [DrawRectangle] Draws a square shape by a coordinate pair, a width, and a height.
- [DrawRectangles] Draws multiple rectangles by Rectangle structures.
- [DrawString] Draws a text string at the specified location, and specified Brush and Font.
Share/Bookmark

No comments: