![](/user_photo/1438_p9ksI.png)
Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf![](/html/1438/356/html_idNWWzYgz4.snxj/htmlconvd-pA35jt351x1.jpg)
![](/html/1438/356/html_idNWWzYgz4.snxj/htmlconvd-pA35jt352x1.jpg)
INTRODUCTION TO GDI+, EVENTS, AND DELEGATES |
Chapter 11 |
|
321 |
|
|
||||
|
|
|
|
|
Imaging. Certain pictures, such as pictures displayed as icons, cannot be represented as a series of lines and curves. The same is applicable for high-resolution pictures. To store such images, you need to store them as bitmaps. The Bitmap class of GDI+ can be used to display and save such bitmaps.
2-D and 3-D vector graphics. GDI+ allows 2-D and 3-D vector graphics. Vector graphics are used to draw lines and curves that can be represented by a set of points on a coordinate system. For example, to specify a rectangle, you can specify its upper-left corner and a pair of numbers giving its width and height. GDI+ consists of various classes that store information about the lines and curves and perform the actual drawing. For example, the Rectangle class can be used to store the information about a rectangle, and the Pen class can be used to store information about the line color, width, style, etc.
You can use a metafile to record a vector image. The Metafile class in GDI+ can be used to record and save a metafile. Other classes related
to the Metafile class, such as the MetafileHeader and the MetaHeader,
can be then used to monitor the data stored in the metafile header.
NOTE
A vector image is nothing but a series of graphics commands.
Typography. Typography deals with the display of information in different fonts, and styles. GDI+ supports a range of features that ensure typography.
Having discussed the various services offered by GDI+, I will now discuss the new features of GDI+ in the following section.
New Features of GDI+
As stated earlier, GDI+ is an extension of GDI. However, GDI+ is different from GDI in a number of ways. The various new features in GDI+ are as follows:
Linear and horizontal gradient brushes
![](/html/1438/356/html_idNWWzYgz4.snxj/htmlconvd-pA35jt353x1.jpg)
322Project 2 CREATING AN APPLICATION USING MANAGED C++
Independent path objects
Cardinal splines
The Matrix object
Transformations to regions
Alpha blending
Support for different image formats
I will now discuss each one of them in detail.
Linear and Horizontal Gradient Brushes
GDI+ provides the linear gradient brush and the path gradient brush for filling regions of different styles. You can also use these brushes to fill a shape with color that changes as you move the brush across the shape. For example, you can create a horizontal gradient brush by specifying yellow at the left edge of a shape and green at the right edge of the shape. As you fill the shape by moving the brush from left to right, the color of the shape will change gradually from yellow to green. You can also create a vertical gradient brush in the same manner. The color of the shape will change as you move the brush from top to bottom.
Similarly, you may want to change the color of a shape as the brush is moved from the edges toward the center or vice versa.
Independent Path Objects
A path is a sequence of connecting straight lines. Every path belongs to a device context in GDI. A path is destroyed as it is drawn. You use the Graphics object in GDI+ to perform drawing. You can also create several GraphicsPath objects. A GraphicsPath object is not destroyed after the drawing action; therefore, you can use the same GraphicsPath object to draw a path umpteen number of times.
Cardinal Splines
A cardinal spline is a set of individual curves joined to form a bigger curve. A spline is therefore specified as an array of points. A cardinal spline traverses smoothly through each point in the array and is therefore more polished than a path created by connecting straight lines.
![](/html/1438/356/html_idNWWzYgz4.snxj/htmlconvd-pA35jt354x1.jpg)
INTRODUCTION TO GDI+, EVENTS, AND DELEGATES |
Chapter 11 |
|
323 |
|
|
||||
|
|
|
|
|
The Matrix Object
You can use the Matrix object in GDI+ to make transformations of objects simple. You can use the Matrix object in conjunction with the objects that need transformation.
Transformations to Regions
A region is a part of a display area. In GDI, the only transformation that you can apply to a region is to translate it. However, things are a little different in GDI+. You can apply a range of transformations in GDI+ such as scaling, rotating, and translating. GDI stores the regions in device coordinates whereas GDI+ stores regions in world coordinates.
Alpha Blending
Another feature of GDI+ that you can use to work with graphics is to specify the transparency of a fill color. This method is known as alpha blending. The transparency of a fill color refers to the degree by which the background color is visible through the fill color. In alpha blending, a transparent color is blended with the background color. You can also specify different levels of transparency to colors.
TIP
The more transparent is the fill color, the more the background color is visible.
Support for Different Image Formats
GDI+ supports a variety of images, such as BMP, GIF, JPEG, EMF, PNG, TIFF, ICON, and so on, through the Image, Bitmap, and Metafile classes. These classes help you save and manipulate images of different formats.
I have discussed the new features of GDI+ as compared to GDI. In the next section, I discuss the various changes in the programming model in GDI+.
![](/html/1438/356/html_idNWWzYgz4.snxj/htmlconvd-pA35jt355x1.jpg)
INTRODUCTION TO GDI+, EVENTS, AND DELEGATES |
Chapter 11 |
|
325 |
|
|
||||
|
|
|
|
|
ters. For example, the FillPolygon method of the Graphics class can take the following forms:
Overloads Public Sub FillPolygon (Brush, Point())
Overloads Public Sub FillPolygon (Brush, PointF())
Overloads Public Sub FillPolygon (Brush, Point(), FillMode)
Overloads Public Sub FillPolygon (Brush, PointF(), FillMode)
Different Methods for Drawing and Filling
GDI+ offers a range of solutions for drawing the outlines and filling the shape. Consider an example where you want to draw and fill a rectangle. In GDI, you can directly use the Rectangle function that takes care of drawing and filling the rectangle with the specified color. The rectangle is drawn with the currently selected pen and the rectangle is filled with the currently selected brush.
In GDI+, you need to use two separate functions of the Graphics class, DrawRectangle and FillRectangle, for drawing the outline of a rectangle and filling the rectangle, respectively. One of the arguments of the DrawRectangle method, the Pen object, is used to draw the outline of the rectangle. Similarly, one of the arguments of the FillRectangle method, the Brush object, is used to fill the rectangle.
Creating Regions
You can use the various functions provided by GDI+, such as CreateRectRgn,
CreateEllpticRgn, CreateRoundRectRgn, CreatePolygonRgn, and CreatePoly-
PolygonRgn, to create regions. The Region class provides a constructor that can receive a Rectangle object and a GraphicsPath object. Consider a situation where you want to construct a region based on a polygon. To do this, you need to create a GraphicsPath object that contains the polygon and pass this GraphicsPath object to the Region constructor.
You can also combine different shapes and paths to create complex regions by using the Union and Intersect methods of the Region class. An important point to note here is that the GraphicsPath object is not destroyed after it is passed to the Region constructor or when it is passed as an argument to the Union or Intersect method. However, in GDI the path gets destroyed when you convert a path to a region by using the PathToRegion function. Therefore, the same path can be used as a building block for different regions in GDI+.
![](/html/1438/356/html_idNWWzYgz4.snxj/htmlconvd-pA35jt357x1.jpg)
326 Project 2 CREATING AN APPLICATION USING MANAGED C++
I have discussed the various programming model changes in this section. At this stage, it is now important for you to learn about the different classes and namespaces in GDI+.
Important Classes and Namespaces in
GDI+
The following is a list of classes and namespaces that will be described in this section:
The System.Drawing namespace
The Graphics class
The Brush class
The Pen class
I will now discuss each of them in detail.
The System.Drawing Namespace
This namespace is used to provide access to the basic graphics functionality in
GDI+. The System.Drawing.Drawing2D, System.Drawing.Imaging, and Sys-
tem.Drawing.Text namespaces can be used to provide advanced functionality. The classes that constitute the System.Drawing namespace are listed in Table 11-1.
Table 11-1 |
Classes in the System.Drawing Namespace |
Class |
Description |
|
|
Bitmap |
Encapsulates a bitmap that consists of the pixel data for a |
|
graphics object. (Recall that a Bitmap object is an object that |
|
is used to work with images defined by pixel data.) |
Brush |
Acts as a base class. Classes derived from this class can be used |
|
to define objects that can fill the various graphical shapes with |
|
specified colors. |
Brushes |
Specifies the brushes for all the standard colors. This class |
|
cannot be inherited. |
![](/html/1438/356/html_idNWWzYgz4.snxj/htmlconvd-pA35jt358x1.jpg)
![](/html/1438/356/html_idNWWzYgz4.snxj/htmlconvd-pA35jt359x1.jpg)
328 |
Project 2 |
CREATING AN APPLICATION USING MANAGED C++ |
|||
|
|
|
|
|
|
|
|
Table 11-1 (continued) |
|
||
|
|
|
|
|
|
|
|
Class |
|
Description |
|
|
|
|
|
|
|
|
|
SolidBrush |
Used to define a brush. This class cannot be inherited. |
||
|
|
StringFormat |
Defines the text layout and display information. This class |
||
|
|
|
|
|
cannot be inherited. |
|
|
SystemBrushes |
Each property of this class is a SolidBrush object. |
||
|
|
SystemIcons |
Each property of this class is an Icon object. This class cannot |
||
|
|
|
|
|
be inherited. |
|
|
SystemPens |
Each property of this class is a Pen object. |
||
|
|
TextureBrush |
Each property of this class is a Brush object. This class cannot |
||
|
|
|
|
|
be inherited. |
|
|
ToolboxBitmapAttribute |
Consists of objects that define the images for a specified |
||
|
|
|
|
|
component. |
|
|
|
|
|
|
The System.Drawing namespace also consists of structures in addition to classes. The structures in the System.Drawing namespace are listed in Table 11-2.
Table 11-2 Structures in the System.Drawing Namespace
Structure Name |
Description |
CharacterRange |
Defines a range of character positions. |
Color |
Represents an RGB color. |
Point |
Represents an ordered pair of integer coordinates that defines |
|
a point in a two-dimensional space. |
PointF |
Represents an ordered pair of float coordinates. |
Rectangle |
Used to store the information for a rectangular region. |
RectangleF |
Used to store the information for a rectangular region in float- |
|
ing point. |
Size |
Used to represent the size for a rectangular region. |
SizeF |
Used to represent the size for a rectangular region in floating |
|
point. |
|
|
![](/html/1438/356/html_idNWWzYgz4.snxj/htmlconvd-pA35jt360x1.jpg)