GDI+: The Next-Generation Graphics Interface


Welcome to the graphics world of GDI+, the next-generation graphics devices interface. GDI+ is the gateway to interact with graphics device interfaces in the .NET Framework. If you're going to write .NET applications that interact with graphics devices such as monitors, printers, or files, you will have to use GDI+.

This article will introduce GDI+. First we will discuss the theoretical aspects of GDI+, which you should know before starting to write a graphics applications.

After reading the article, you should understand the followings topics:

  • What is GDI+ is?
  • How GDI+ is defined?
  • How to use GDI+ in your applications?
  • What's new in GDI+?
  • What the major programming differences between GDI and GDI+ are?
  • Which major namespaces and classes in the .NET Framework library expose the functionality of GDI+?
Understanding GDI+

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 designed, and how it can be used in managed and unmanaged applications.

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), which 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 monument 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 illustrates this process.

Figure1.1 The role of GDI+.jpg

FIGURE 1: The role of GDI+

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.

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.

GDI Interoperability

You can use GDI in managed application with GDI+. GDI interoperability allows you to use GDI functionality in managed applications with GDI+, but you need to take some precautions.

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 concept of managed and unmanaged code.

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 this is not being managed by the CLR is called unmanaged code.

Managed code enjoys many rich features provide 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 garbage collector (GC), the CLR automatically manages the life cycle of the 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 the objects programmatically.

You can write both managed and unmanaged applications using Microsoft Visual Studio .NET. You can use Visual C++ 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.

GDI+ 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.

Figure 1.2.jpg

FIGURE 2: The managed GDI+ class wrapper

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.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.

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.Drawing2D, System.Drawing.Imaging, System.Drawing.Printing, and System.Drawing.Text.

Conclusion

Hope this article would have helped you in understanding GDI+ basics. See my other articles on the website on GDI+ which are in continuation of this article.