Understanding GDI+

Table of contents:

If you want to write efficient and optimized graphics applications, it's important to understand the GDI+ class library. In this section we will discuss how GDI+ is defined, and how it can be used in managed and unmanaged applications.

1.1.1 Definition

GDI+ is a library that provides an interface that allows programmers to write Windows and Web graphics applications that interact with graphical devices such as printers, monitors, or files.

All graphical user interface (GUI) applications interact with a hardware device (a monitor, printer, or scanner), that can represent the data in a human-readable form. However, there is no direct communication between a program and a device; otherwise, you would have to write user interface code for each and every device with which your program interacts!

To avoid this monumental task, a third component sits between the program and device. It converts and passes data sent by the program to the device and vice versa. This component is the GDI+ library. Typing a simple "Hello World" on the console, drawing a line or a rectangle, and printing a form are examples in which a program sends data to GDI+, which converts it for use by a hardware device. Figure 1.1 illustrates this process.

Figure 1.1. The role of GDI+

graphics/01fig01.gif

Now let's see how GDI+ works. Suppose your program draws a line. A line is displayed as a set of pixels drawn in sequence from the starting location to the ending location. To draw a line on a monitor, the monitor needs to know where to draw the pixels. Instead of telling the monitor to draw pixels, your program calls the DrawLine method of GDI+, and GDI+ draws the line from point A to point B. GDI+ reads the point A and point B locations, converts them to a sequence of pixels, and tells the monitor to display the sequence of pixels.

GDI+ allows you to write device-independent managed applications and is designed to provide high performance, ease of use, and multilingual support.

1.1.2 What Is GDI+?

The previous section defined GDI+. But how is it implemented? GDI+ is a set of C++ classes that are located in a class library called Gdiplus.dll. Gdiplus.dll is a built-in component of the Microsoft Windows XP and Windows Server 2003 operating systems.

Tip

You can use GDI+ on Windows operating systems other than XP. You just need to install GDI+ on the computer, which means that Gdiplus.dll must be copied to the system directory. Installing the .NET SDK, Visual Studio .NET, or .NET redistributable copies Gdiplus.dll automatically.

Comparing GDI+ to GDI, as we do later in this chapter, is a natural way to introduce GDI+. Note, however, that prior knowledge of GDI is not a prerequisite for learning GDI+ or using this book. This book is about GDI+ development in the .NET Framework, which provides new classes and a new way to write graphics applications. Prior experience with GDI will aid your understanding of the basic concepts, but it is not necessary.

GDI Interoperability

You can use GDI in managed applications with GDI+. GDI interoperability allows you to use GDI functionality in managed applications with GDI+, but you need to take some precautions. We will discuss GDI interoperability in Chapter 14.

 

1.1.3 The GDI+ Library in the .NET Framework

The previous section said that the GDI+ library is a set of C++ classes that can be used from both managed and unmanaged code. Before we discuss how GDI+ is represented in the .NET Framework library, let's review the concepts of managed and unmanaged code.

1.1.3.1 Managed and Unmanaged Code

Code written in the Microsoft .NET development environment is divided into two categories: managed and unmanaged. In brief, code written in the .NET framework that is being managed by the common language runtime (CLR) is called managed code. Code that is not being managed by the CLR is called unmanaged code.

Managed code enjoys many rich features provided by the CLR, including automatic memory management and garbage collection, cross-language integration, language independence, rich exception handling, improved security, debugging and profiling, versioning, and deployment. With the help of a garbage collector (GC), the CLR automatically manages the life cycle of objects. When the GC finds that an object has not been used after a certain amount of time, the CLR frees resources associated with that object automatically and removes the object from the memory. You can also control the life cycle of objects programmatically.

You can write both managed and unmanaged applications using Microsoft Visual Studio .NET. You can use Visual C++ 7.0 to write unmanaged code in Visual Studio .NET. Managed Extensions to C++ (MC++) is the way to write C++ managed code. Code written using C# and Visual Basic .NET is managed code.

1.1.3.2 GDI+ in Managed Code

GDI+ exposes its functionality for both managed and unmanaged code. As noted earlier, GDI+ is a set of unmanaged C++ classes. Programmers targeting unmanaged code can use these C++ classes to write their graphics applications.

Note

This book targets only managed code development. Unmanaged GDI+ development will not be discussed.

The .NET Framework library provides managed classes that are a nice wrapper around GDI+ C++ classes. The GDI+ managed classes provided by the .NET Framework library are defined in the System.Drawing.dll and System.Drawing.Design.dll assemblies. Figure 1.2 shows a conceptual diagram of the communication between managed Windows and Web applications and display devices through managed GDI+. As the diagram shows, the managed GDI+ classes defined in the System.Drawing namespace and its subnamespace are a wrapper around the GDI+ C++ classes defined in the Gdiplus.dll unmanaged library.

Figure 1.2. The managed GDI+ class wrapper

graphics/01fig02.gif

The managed GDI+ classes provided in the .NET Framework library are defined in the System.Drawing namespace and its five subnamespaces: System.Drawing.Design, System.Drawing.Drawing2D, System.Drawing.Imaging, System.Drawing.Printing, and System.Drawing.Text. We will discuss these namespaces and their classes in more detail in Section 1.4.

1.1.3.3 GDI+ Revisited

In brief,

  • GDI+ is a component that sits between an application and graphical devices. It converts data into a form compatible with a graphical device, which presents the data in human-readable form.
  • GDI+ is implemented as a set of C++ classes that can be used from unmanaged code.
  • In the .NET Framework library, GDI+ classes are exposed through System.Drawing (and its subnamespaces), which provides a managed class wrapper around the GDI+ C++ classes.

In this book we will be using GDI+ through the namespaces provided by the .NET Framework library. If you want to learn more about GDI+ C++ classes, search for GDI+ references on MSDN. On the GDI+ references page (go to http://msdn.microsoft.com/library, expand Graphics and Multimedia, and then click on GDI+), you can find all GDI+ classes, functions, constants, enumerations, and structures.

1.1.4 What's New in GDI+ for GDI Programmers?

GDI+ provides significant improvements over its predecessor, GDI. In this section we will take a quick look at these improvements.

GDI+ provides some nice features for 2D vector graphics. One of the many nice features is support for floating point coordinates. For example, the PointF, SizeF, and RectangleF classes represent a floating point, size, and rectangle, respectively. Other objects that use Point, Size, and Rectangle objects also have overloaded methods that can use the PointF, SizeF, and RectangleF objects.

The alpha component, which represents the opacity of a color, is a new addition to the Color structure. Alpha blending, anti-aliasing, and color blending are other new additions to the library. We will discuss these topics in more detail in Chapters 5 and 9.

Texture and gradient brushes are another new addition. Some other additions to the basic primitives are compound lines, cardinal splines, scalable regions, inset pens, high-quality filtering and scaling, and many new line styles and line cap options.

Imaging is another area where GDI developers will find many new additions in GDI+. Some of the additions are native support for image file formats such as .jpeg, .png, .gif, .bmp, .tiff, .exif, and .icon; support for encoding and decoding raster formats; native image processing support; brightness, contrast, and color balance; and support for transformations, including rotation and cropping.

In color management, support for sRGB, ICM2, and sRGB64 is a new addition. Typography support includes the ClearType, texture, and gradient-filled texts, as well as support for Unicode and Windows 2000 scripts.

GDI+: The Next-Generation Graphics Interface

Your First GDI+ Application

The Graphics Class

Working with Brushes and Pens

Colors, Fonts, and Text

Rectangles and Regions

Working with Images

Advanced Imaging

Advanced 2D Graphics

Transformation

Printing

Developing GDI+ Web Applications

GDI+ Best Practices and Performance Techniques

GDI Interoperability

Miscellaneous GDI+ Examples

Appendix A. Exception Handling in .NET



GDI+ Programming with C#
GDI+ Programming with C#
ISBN: 073561265X
EAN: N/A
Year: 2003
Pages: 145

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net