GDI+ is an improved version of Microsoft's graphics device interface (GDI) API. In this chapter we learned how GDI+ is designed for use in both managed and unmanaged code.
and its helper namespaces defined in the .NET Framework library provide a managed class wrapper to write managed GDI+ applications. We also learned the basics and definition of GDI+ and what major improvements are
Now that you've learned the basics of GDI+, the
Chapter 2. Your First GDI+ Application
In this chapter we move to the more practical aspects of writing graphics applications using GDI+ in the .NET Framework. This chapter is the foundation chapter and discusses
To write a graphics application, a good understanding of drawing surfaces and coordinate systems is necessary. We will begin by discussing these concepts and how they are represented in GDI+. Then you'll learn step-by-step how to write a graphics application in the .NET Framework using GDI+. We will cover the following topics:
At the end of this chapter we will discuss some basic graphics structures and their
2.1 Drawing Surfaces
Every drawing application (regardless of the operating system), consists of three common
To draw graphics objects you need to have a pen or a brush, which defines the texture, color, and width of the drawing. For example, if you draw a line or a rectangle, you need to create a pen with a color and width.
The process component of the drawing application includes making a call to draw the line or rectangle on the form.
Each drawing surface has four common properties: width, height, resolution, and color depth.
A pixel is the smallest element that participates in the drawing process to display graphics objects or images on the screen. The pixel density is often represented by a value in dots per inch (dpi).
The quality of a pixel is directly proportional to the color depth. The
structure represents a color in GDI+. It has four components: alpha, red, green, and blue. The
of a color represent the number of possible colors (see Figure 2.1). Each component in RGB has 256 (2
) color combinations. Hence all three components of GDI+ color represent 256x256x256 possible colors. The
determines the transparency of the color, which affects how the color
Figure 2.1. Color components in GDI+
To see the proper colors defined in the GDI+ color structure, a drawing surface must support at least a 24-bit color system (for the RGB components of a color structure), which means that each pixel of the surface must be able to hold 24 bits (8 bits each for the R, G, and B components, as noted already). Surfaces with less than 24 bits per pixel may not display graphics objects and images exactly as defined in a drawing application. We will discuss colors in more detail in Chapter 5.
The color depth of a surface is different from the color depth of a particular display device, such as a monitor or a printer. Most monitors can support over a million colors, and some printers may support only black and white.
GDI+ provides three types of drawing surfaces: forms, printers, and bitmaps.
2.1.1 Forms as a Surface
When you write a Windows application that draws something on a form, the form acts as a drawing surface and supports all the properties required by a drawing surface.
2.1.2 Printers as a Surface
When you print from an application, the printer acts as a drawing surface. You can set a printer's resolution and color depth, as well as the height and width of the paper. We will discuss printer-
2.1.3 Bitmaps as a Surface
When you create images in memory and save them as a bitmap, the bitmap functions as a drawing surface. You can set the image width, height, resolution, and color depth properties. Bitmap surfaces are commonly used for writing graphics Web applications. Drawing works a little differently in Web applications. For example, if you want to draw a line and a rectangle in a Web page using GDI+, you need to create an image, use this image as a surface for the line and rectangle objects, set its surface-related properties, and then send the image to the browser. We will discuss Web graphics applications in more detail in Chapter 12.