Return to Index
created by gbSnippets
Microsoft upgraded the GDI with GDI+. GDI+ is the graphical library used in
the .NET Framework.
GDI+ is inherently installed in Microsoft Windows XP, Windows Server 2003, and
Windows Vista. To use it on previous operating systems, it must be explicitly
distributed. GDI+ provides its functionality through three fronts:
This is the area that consists of drawing and manipulating geometric-based and
related figures including lines, combinations of lines, round and quadrilateral
shapes. These are treated as sets of points on a screen or other device. To
perform these types of operations, the GDI+ system provides various classes
that perform different assignments. For example, one class can be in charge of
creating or preparing tools used to draw. Another class can be used to perform
the actual drawing, using the provided tools
While it may appear easy to create vector graphics that are made of easily
recognizable colors, advanced pictures present a challenge to display or
draw them on a device. For these reasons, imaging is the area used to deal
with such complex operations
Typography consists of creating, manipulating or making fonts available to an
To draw in GDI, you have to obtain a handle to 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.
What is a namespace?
A namespace is a method of organizing a group of assemblies, classes, or types.
A namespace acts as a container—like a disk folder—for classes organized into
groups usually based on functionality. C# namespace syntax allows namespaces to
For instance, to access the built-in input-output (I/O) classes and members,
use the System.IO namespace. Or, to access Web-related classes and members,
use the System.Web namespace.
All C# programs should call the System namespace—the father of all .NET Framework
A namespace is similar to a Java package. However, whereas Java package names
dictate the source files directory structure, C# namespaces dictate only the
The C++ namespace syntax is similar to the C# syntax.
The main object on which you will perform most drawings is called a graphic.
In most cases, this object is not readily available when you need it: you
must request it from the object on which you want to draw or you must create it.
Both operations are highly easy.
Getting a Graphic Object
In GDI+, a graphic object is based on a class called Graphics. This class is
defined in the System.Drawing namespace. 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 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 for 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.
Two other pieces of information are particularly important with regards to any
figure or shape you will need to draw: its location and dimensions.
The Starting Point of a Shape or Figure
To keep track of the various drawings, the object on which you draw uses a
coordinate system that has its origin (0, 0) on its top-left corner. If you
are drawing on a form, this origin is positioned just under the title bar to
The Bitmap Class
To support bitmaps, the GDI+ library provides the Bitmap class. In the .NET
Framework, the bitmap is represented by a class called Bitmap. The Bitmap
class is derived from the Image abstract class. Both classes are defined
in the System.Drawing namespace of the System.Drawing.dll assembly. The
Bitmap class is serializable.
Creating a Graphics From a Bitmap
In our introduction to graphics, we saw various ways of getting a getting a
Graphics object, either from the CreateGraphics() method of a Control-derived
class or from the handle of the control. Besides these techniques, the Graphics
class provides a method named FromImage. Its syntax is:
Public Shared Function FromImage(image As Image) As Graphics
This shared method takes as argument a variable of type Image. Therefore, when
calling this method, pass an Image or an Image-based variable to it. After the
method has been called, it produces a Graphics object.
Presenting a Bitmap
Once the picture is ready, to present it to the user, for example to display
it in your application, you can call the Graphics.DrawImage() method that is
overloaded with as many different versions as you can possibly need. One of
the versions of this method has the following syntax:
Public Sub DrawImage(image As Image, x As Integer, y As Integer)
The first argument can be a bitmap that you may have previously initialized.
The second argument specifies the location where the picture will be drawn.
Size / Location of Bitmap
When creating or designing a Bitmap object, you can specify its primary size.
To do this, you can use the following constructor:
Public Sub New(width As Integer, height As Integer)
To support picture transparency, the Bitmap class is equipped with the
MakeTransparent() method that is overloaded with two versions. The first
version uses the following syntax:
Public Sub MakeTransparent
Instead of using the default transparency color of the operating system, you
can specify your own color. To support this, the Bitmap class provides another
version of the MakeTransparent() method. Its syntax is:
Public Sub MakeTransparent(transparentColor As Color)
To support picture scaling, the Bitmap class provides the following constructor:
Public Sub New(original As Image, width As Integer, height As Integer)
The original argument is the Image, such as a Bitmap object, that you want to
scale. The width and the height arguments represent the new size you want to
apply to the picture. After this constructor has been used, you get a bitmap
with the new size. Here is an example of using it:
To support mirroring, the Bitmap class inherits a method named RotateFlip from its parent Image class. Its syntax is:
Public Sub RotateFlip(rotateFlipType As RotateFlipType)
This function takes one argument that specifies the mirroring option through the
RotateFlipType enumeration. The members of the RotateFlipType enumeration that
can be used to mirror a picture are RotateNoneFlipX and Rotate180FlipY. Here is
an example of mirroring a picture:
Flip A Picture
To support picture flipping, you can call the same RotateFlip() method of the
Image class. This time, you would use a different value for the argument. The
member of the RotateFlipType enumeration used to flip a picture is RotateNoneFlipY.
Here is an example of flipping a picture:
Like other major Microsoft technologies, the term "GDI+" is really a whole group
of software technologies. The individual groups are often called "namespaces" and
the ones that are in GDI+ are:
This is THE core GDI+ namespace. It defines objects for basic rendering (fonts,
pens, basic brushes, etc.) and the most important object: Graphics. 'll see
more of this in just a few paragraphs.
This gives you objects for more advanced two-dimensional vector graphics. Some
of them are gradient brushes, pen caps, and geometric transforms.
If you want to change graphical images - that is, change the palette, extract
image metadata, manipulate metafiles, and so forth - this is the one you need.
To render images to the printed page, interact with the printer itself, and
format the overall appearance of a print job, use the objects here.
You can use collections of fonts with this namespace.
created by gbSnippets