Home

Basic Tools of GDI+

 

Introduction

To support GDI+ graphics and their features, the .NET Framework provides the System::Drawing namespace that is is created in the System.Drawing.dll library. This namespace also contains classes to draw or define a font in an application. To enhance the aspects of a drawing, the .NET Framework provides additional classes in the System::Drawing::Drawing2D namespace. This namespace also is defined in the System.Drawing.dll assembly. To support addition manipulation techniques that can applied to a picture, the .NET Framework provides some other classes in the System::Drawing::Imaging namespace, which is also part of the System.Drawing.dll library.

 

The Graphics Platform

To draw in GDI, you have to obtain a handle to the device context. This is done by declaring a variable or a pointer to HDC then calling a function such as BeginPaint() to initialize the device context. You also have to create the tools needed to draw. For example, you have to create a pen and/or a brush. Once the tools are ready, you have to select them into the device context to make them available. After drawing, it is suggested that you release the device context.

To draw in GDI+, you use an object referred to as graphic.

The Color To Fill

The color is one of 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. The Color structure is defined in the System::Drawing namespace.

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:

Bits
Alpha
7 6 5 4 3 2 1 0
Red 
7 6 5 4 3 2 1 0
Green 
7 6 5 4 3 2 1 0
Blue 
7 6 5 4 3 2 1 0

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

27 + 26 + 25 + 24 + 23 + 22 + 21 + 20 

= 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1

= 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:

Color 
23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Blue Green Red
Value

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 horizontal line and a vertical line is called a pixel. This pixel holds, carries, or displays one color:

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:

System::Void Form1_Load(System::Object^  sender,
				 System::EventArgs^  e)
{
    BackColor = Color::Turquoise;
}

If none of the pre-defined colors suits you, you can define your own color 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);
    static Color FromArgb(int alpha, Color baseColor);
    static Color FromArgb(int red, int green, int blue);
    static Color FromArgb(int alpha, int red, int green, int blue);

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

System::Void Form1_Load(System::Object^  sender,
				 System::EventArgs^  e)
{
    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

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 Pen handle. The primary 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:

System::Void Form1_Load(System::Object^  sender,
				 System::EventArgs^  e)
{
    Color clrBlue = Color::Blue;
    Pen ^ penRed = gcnew 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.

 

Previous Copyright © 2007-2013, FunctionX Next