GDI+ is next evolution of GDI for the .Net Platform. All GDI+ classes reside in the following namespaces: | ||||||||||||||||||||||||||||||||||||||||||
• System.Drawing
• System.Text • System.Printing • System.Internal • System.Imaging • System.Drawing2D • System.Design |
||||||||||||||||||||||||||||||||||||||||||
The Graphics Class | ||||||||||||||||||||||||||||||||||||||||||
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: | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
In .NET, GDI+ functionality resides in the System.Drawing.dll. Before you start using GDI+ classes, you must add reference to the System.Drawing.dll and import System.Drawing namespace. If you are using Visual Studio .NET for your development, you can add reference to the GDI+ library using following: | ||||||||||||||||||||||||||||||||||||||||||
Creating this project is simple. Create a Windows application and add reference to the System.Drawing.dll using Project->Add Reference menu item. See the Figure below: | ||||||||||||||||||||||||||||||||||||||||||
After that add these two lines. | ||||||||||||||||||||||||||||||||||||||||||
using System.Drawing; using System.Drawing.Drawing2D; |
||||||||||||||||||||||||||||||||||||||||||
Note: If you create a Windows application using VS.NET, you only need write only one line. | ||||||||||||||||||||||||||||||||||||||||||
using System.Drawing.Drawing2D; | ||||||||||||||||||||||||||||||||||||||||||
After that add a Form_Paint event handler using the Properties Window. See the Figure below: | ||||||||||||||||||||||||||||||||||||||||||
Note: You can also add Form paint event handler manually described above. | ||||||||||||||||||||||||||||||||||||||||||
Graphics Objects | ||||||||||||||||||||||||||||||||||||||||||
After creating a Graphics object, you can use it draw lines, fill shapes, draw text and so on. The major objects are: | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
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. | ||||||||||||||||||||||||||||||||||||||||||
public Pen(Color); | ||||||||||||||||||||||||||||||||||||||||||
Initializes a new instance of the Pen class with the specified Brush. | ||||||||||||||||||||||||||||||||||||||||||
public Pen(Brush); | ||||||||||||||||||||||||||||||||||||||||||
Initializes a new instance of the Pen class with the specified Brush and width. | ||||||||||||||||||||||||||||||||||||||||||
public Pen(Brush, float); | ||||||||||||||||||||||||||||||||||||||||||
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 ); OR Pen pn = new Pen( Color.Blue, 100 ); |
||||||||||||||||||||||||||||||||||||||||||
Some of its most commonly used properties are: | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
The Color Structure | ||||||||||||||||||||||||||||||||||||||||||
A Color structure represents an ARGB color. Here are ARGB properties of it: | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
You can call the Color members. Each color name (say Blue) is a member of the Color structure. Here is how to use a Color structure: | ||||||||||||||||||||||||||||||||||||||||||
Pen pn = new Pen( Color.Blue ); | ||||||||||||||||||||||||||||||||||||||||||
The Font Class | ||||||||||||||||||||||||||||||||||||||||||
The Font class defines a particular format for text such as font type, size, and style attributes. You use font constructor to create a font. | ||||||||||||||||||||||||||||||||||||||||||
Initializes a new instance of the Font class with the specified attributes. | ||||||||||||||||||||||||||||||||||||||||||
public Font(string, float); | ||||||||||||||||||||||||||||||||||||||||||
Initializes a new instance of the Font class from the specified existing Font and FontStyle. | ||||||||||||||||||||||||||||||||||||||||||
public Font(Font, FontStyle); | ||||||||||||||||||||||||||||||||||||||||||
Where FontStyle is an enumeration and here are its members: | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
Here is one example: | ||||||||||||||||||||||||||||||||||||||||||
Graphics g ; Font font = new Font(“Times New Roman”, 26); |
||||||||||||||||||||||||||||||||||||||||||
Some of its most commonly used properties are: | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
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: | ||||||||||||||||||||||||||||||||||||||||||
LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal); | ||||||||||||||||||||||||||||||||||||||||||
OR | ||||||||||||||||||||||||||||||||||||||||||
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 Structure | ||||||||||||||||||||||||||||||||||||||||||
public Rectangle(Point, Size); OR public Rectangle(int, int, int, int); |
||||||||||||||||||||||||||||||||||||||||||
The Rectangle structure is used to draw a rectangle on WinForms. Besides its constructor, the Rectangle structure has following members: | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
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 Point Structure | ||||||||||||||||||||||||||||||||||||||||||
This structure is similar to the POINT structure in C++. It represents an ordered pair of x and y coordinates that define a point in a two-dimensional plane. The member x represents the x coordinates and y represents the y coordinates of the plane. | ||||||||||||||||||||||||||||||||||||||||||
Here is how to instantiate a point structure: | ||||||||||||||||||||||||||||||||||||||||||
Point pt1 = new Point( 30, 30); Point pt2 = new Point( 110, 100); |
||||||||||||||||||||||||||||||||||||||||||
|