ost Windows applications need to use graphics or images to let users work with the images directly, or to enhance the user experience. Most applications can accomplish this with static image data, without modifying the image files or manipulating the data on screen. For some applications such as a document management system that may need to render an underlying graphics file for a scanned document in many different ways, the need to modify the image data is critical. With GDI+, the Windows graphic design interface, and the .NET Framework you can develop applications to manipulate image data to suit your needs. You'll find the classes and methods you need in the .NET Framework in the System.Drawing namespace.
|What You Need
|To build the example included with this article, you should have a basic understanding of the .NET Framework and C#. You'll need Visual Studio .NET and the .NET Framework SP1.
To leverage GDI+ and its supported imaging functions in your applications, you should develop a basic understanding of GDI+ and how the .NET framework supports its imaging functionality. You should also familiarize yourself with the Image, Graphics, and Bitmap classes available in the System.Drawing and System.Drawing.Imaging namespaces. These classes support image data management and modification. In this article, you'll see how to construct a sample application that uses these classes to load, modify, and save image data. Specifically, the sample code demonstrates image conversion, placing text on an image, cropping, and image rotation. You'll also see how to apply these techniques in a Windows forms application to display and manipulate images dynamically.
GDI+ supports several image file formats natively, including jpeg, bmp, gif, tiff and others, as well as common image transformation operations such as cropping, rotation, flipping, and image adjustments for contrast, brightness, and blurring. The architecture is extensible, so you can add new image formats if necessary.
Important GDI+ Classes
The Image class supports numerous functions for dealing with image data. The Bitmap class, derived from the Image class, holds the pixel data for an image and also manages related image attributes. You'll most commonly dealing with pixel-based raster images by creating a Bitmap instance. In the System.Drawing.Imaging namespace, you can use the ImageFormat class to specify the image format. The Image class uses an ImageFormat instance as an input parameter to specify the format for functions that save images or translate images between formats.
While the Bitmap and Image classes support much of the native imaging functions, some manipulations require you to use the drawing functions available through the Graphics class. Specifically, you can use the Graphics.DrawImage method to render an image based on an instance of an Image object, drawing it to a specified location at a specified size based on the input parameters provided to the function. The sample Windows forms application that accompanies this article demonstrates the power of these imaging functions for image manipulations.