Introduction to GDI+


Device Contexts


Introduction to GDI





The GDI+

GDI+ is the system used to perform drawing and other related graphics operations for the Microsoft Windows family of operating system. Its predecessor was the Graphical Device Interface (GDI), which has therefore been replaced, namely with the new operating systems such as Windows XP and Windows Server 2003. The + in GDI+ indicates that it provides a significant improvement to GDI and adds new features that were not available in GDI and were therefore difficult to produce. GDI+ allows you to create device-independent applications without worrying about the hardware on which the application would run.

GDI+ is inherently installed in Microsoft Windows XP and Windows Server 2003. To use it on previous operating systems, it must be explicitly distributed. GDI+ provides its functionality through three fronts:

  • Vector Graphics
  • Imaging
  • Typography
    Typography consists of creating, manipulating or making fonts available to an application.


Basic Tools of GDI+


The Graphics Platform


To draw in GDI+, you use an object called a graphic or a graphics object.


The Color To Fill

The color is one the most fundamental objects that enhances the aesthetic appearance of an object. The color is a non-spatial object that is added to an object to modify some of its visual aspects. To support colors, the GDI+ library provides the Color structure.

A color is created as a combination of four 8-bit values. The first value is referred to as alpha but it is mostly used internally. The second is called red. The third is called green. The fourth is called blue:

7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0

Converted to decimal, each one of the red, green, and blue numbers would produce:



= 255

Therefore, each number can have a value that ranges from 0 to 255 in the decimal system. The alpha section is reserved for the operating system. The other three numbers are combined to produce a single value as follows:

Blue Green Red

Converted to decimal, this number has a value of 255 * 255 * 255 = 16581375. This means that we can have approximately 16 million colors available. The question that comes to mind is how we use these colors, to produce what effect.

You computer monitor has a surface that resembles a series of tinny horizontal and vertical lines. The intersection of a one horizontal line and a vertical line is called a pixel. This pixel holds, carries, or displays one color.

Pixel Representation

As the pixels close to each other have different colors, the effect is a wonderful distortion that creates an aesthetic picture. It is by changing the colors of pixels that you produce the effect of color variances seen on pictures and other graphics.

To make color selection easier, the Color structure is equipped with various properties that each represents a name for a color. Therefore, to use any of these colors, call the Color structure followed by the "::" operator, followed by the desired color. All the popular names of colors are recognized and they are represented in the Color structure by static properties. These include Red, Green, Blue, Black, White, Yellow, Fuchsia, Silver, Gray, Brown, and Khaki, etc just to name a few. There are many other colors that are not necessarily popular. Here is an example:

	 this->BackColor = Color::Turquoise;

If none of the pre-defined colors suits you, you can define your own as a combination of red, green, and blue values. To create a color using this approach, you can declare a variable of type Color. To specify the characters of the color, the Color structure provides the FromArgb() static method overloaded in four versions as follows:

public: static Color FromArgb(int argb);
public: static Color FromArgb(int alpha, Color baseColor);
public: static Color FromArgb(int red, int green, int blue);
public: static Color FromArgb(int alpha, int red, int green, int blue);

The third version, which is the most used allows you to specify three value that each ranges from 0 to 255. Here is an example:

	 this->BackColor = Color::FromArgb(26, 69, 174);

This would produce:

Whether a color was initialized with one of the Color pre-defined color properties or using the FromArgb() methods, if you want to retrieve the red, green, and blue values of a color, you can use the R, the G, or the B properties to extract the value of each. Each one of these properties is of a byte type. Alternatively, you can call the Color::ToArgb() method. Its syntax is:

public: int ToArgb();

This method returns an integer.


The Pen

While the Graphics class provides the platform to draw on, you need a tool to draw with. The most basic tool you can use is the pen. The GDI+ library provides a pen through the Pen class. To obtain a pen, you can declare a pointer to Pen. The most basic piece of information you must specify about a pen is its color. To do this, you can use the following constructor: 

public: Pen(Color color);

Here is an example:

	 Color clrBlue = Color::Blue;
	 Pen *penRed = new Pen(clrBlue);

If you have already created a pen, to change its color, you can assign the desired color name or color value to the Pen::Color property.

The Pen class provides more details about a pen than that. For now, we can use a pen as simple as this one.

The Graphics Object




Getting a Graphic Object

In GDI+, a graphic object is based on a class called Graphics. Therefore, before drawing, you should obtain a graphic object. Fortunately, every Windows control, that is, every object based on the Control class automatically inherits a method called CreateGraphics(), which gives you access to the graphic part of a control. The syntax of the Control::CreateGraphics() method is:

public: Graphics *CreateGraphics();


	 Graphics *graph = this->CreateGraphics();

Another technique you can use to get the Graphics object of a control is to call the Control::FromHwnd() static method. Its syntax is:

public: static Graphics *FromHwnd(IntPtr hwnd);

Remember that this method is static. The argument passed to it must be a handle to the object whose Graphics object you want to access. We saw already that every Windows control has a handle called Handle. Here is an example of using it to get the Graphics part of a form:

	 Graphics *graph = Graphics::FromHwnd(this->Handle);


private: System::Void Form1_Paint(System::Object *  sender, System::Windows::Forms::PaintEventArgs *  e)
	 e->Graphics . . .

At this time, you should be familiar with the ColorDialog box control.

The Process of Drawing


Getting a Device Context

As mentioned above, before drawing, make sure you have a Graphics object, which depends on your approach to drawing. To actually perform the drawing, the Graphics class provides various methods adapted from different shapes. Each method used to draw something has a name that starts with Draw... Also, each method that is used to draw a known shape requires a Pen argument. Therefore, when drawing, your first decision will be based on the shape or type of figure you want to draw. Probably the second decision will consist on specifying the color of the border.

Two other pieces of information are particularly important with regards to any figure or shape you will need to draw: the location and dimensions.

The Starting Point of a Shape or Figure


The origin of the coordinate system and its axes



Home Copyright © 2004-2012, FunctionX