Previous Section  < Day Day Up >  Next Section

Chapter 8. .NET Graphics Using GDI+

Topics in This Chapter

  • Graphics Overview: The first step in working with GDI+ is to understand how to create and use a Graphics object. This section looks at how this object is created and used to handle the Paint event.

  • Using the Graphics Object to Create Shapes: .NET offers a variety of standard geometric shapes that can be drawn in outline form or filled in. The GraphicsPath class serves as a container that enables geometric shapes to be connected.

  • Using Pens and Brushes: Pens are used to draw shapes in outline form in different colors and widths; a brush is used to fill in shapes and create solid and gradient patterns.

  • Color: Colors may be defined according to red/green/blue (RGB) values or hue/saturation/brightness (HSB) values. Our project example illustrates RGB and HSB color spaces. By representing a color as an object, .NET permits it to be transformed by changing property values.

  • Images: .NET includes methods to load, display, and transform images. The most useful of these is the Graphics.DrawImage method that allows images to be magnified, reduced, and rotated.

Very few programmers are artists, and only a minority of developers is involved in the world of gaming where graphics have an obvious justification. Yet, there is something compelling about writing an application that draws on a computer screen. For one thing, it's not difficult. An array of built-in functions makes it easy to create geometric objects, color them, and even animate them. In this regard, .NET should satisfy the would-be artist that resides in many programmers.

To understand the .NET graphics model, it is useful to look at its predecessor梩he Win32 Graphical Device Interface (GDI). This API introduced a large set of drawing objects that could be used to create device independent graphics. The idea was to draw to a logical coordinate system rather than a device specific coordinate system梖reeing the developer to concentrate on the program logic and not device details. .NET essentially takes this API, wraps it up in classes that make it easier to work with, and adds a wealth of new features.

The graphics classes are collectively called GDI+. This chapter looks at the underlying principles that govern the use of the GDI+, and then examines the classes and the functionality they provide. Several programming examples are included that should provide the tools you will need to further explore the .NET graphics namespaces.

Keep in mind that GDI+ is not restricted to WinForms applications. Its members are also available to applications that need to create images dynamically for the Internet (Web Forms and Web Services).You should also recognize that GDI+ is useful for more than just games or graphics applications. Knowledge of its classes is essential if you want to design your own controls or modify the appearance of existing ones.

    Previous Section  < Day Day Up >  Next Section