One of the most common things you might want to do with GDI+ is display an image that already exists in a file This is actually a lot simpler than drawing your own user interface because the image is already pre-drawn, Effectively, all you have to do is load the file and instruct GDI+ to display it, The image can be a simple line drawing an icon, or a complex image such as a photograph. You can also manipulate the image by stretching or rotating it, or simply displaying only a portion of it.
This section, just for a change, presents the sample first, Then it discusses some of the issues you need to be aware of when displaying images, Presenting it this way is possible because the code needed to display an image is so simple.
The class you need is the .NET base class, System. Drawing. Image. An instance of Image represents one image, Reading in an image simply takes one line of code:
Image myImage = Image.FromFile(“FileNarne’);
FromFile () is a static member of Image and is the usual way of instantiating an image, The file can be any of the commonly supported graphics file formats, including. bmp, . jpg, . gif, and. png.
Displaying an image is also very simple, assuming that you have a suitable Graphics instance at hand – a call to either Graphics. DrawImageUnscaled () or Graphics, DrawImage () suffices, There are quite a few overloads of these methods, allowing you a lot of flexibility in the information you supply in terms of where the image is located and how big it is to be drawn. But this example uses Draw Image ( ), like this:
In this line of code, dc is assumed to be a Graphics instance, and myImage is the Image to be displayed, points is an array of Point structs, where points [0), points [1), and points [2) are the coordinates of the top-left, top-right, and bottom-left comer of the image.
Images are probably the area in which developers familiar with GDI will notice the biggest difference between GDl and GDI+ In GDI, displaying an image involved several nontrivial steps, If the image was a bitmap, then loading it was reasonably simple. Nevertheless, if it was any other file type, then loading it would involve a sequence of calls to OLE objects, Actually getting a loaded image onto the screen required getting a handle to it, selecting it into a memory device coil text, and then performing a block transfer between device contexts, Although the device contexts and handles are still there behind the scenes and will be needed if you want to start doing sophisticated editing of the images from your code, simple tasks have now been extremely well wrapped up in the GDI object model.
The process of displaying an image is illustrated with an example called Display Image, The example simply displays a jpg file in the application‘s main window, To keep things simple, the path of the jpg file is hard-coded into the application (so if you run the example, then you will need to change it to reflect the location of the file in your system), The . jpg file you will display is a sunset picture in St. Petersburg.
As with the other examples, the Display Image project is a standard C# Visual Studio 2008-generated Windows application, You add the following fields to your Forml class:
Note that the size in pixels of the image is obtained as its Size property, which you use to set the document area, You also set up the piccyBounds array, which is used to identify the position of the image on the screen, You have chosen the coordinates oi the three corners to draw the image in its actual size and shape here, but if you had wanted the image to be resized, stretched, or even sheared into a non rectangular parallelogram, then you could do so simply by changing the values of the Points in the piccyBounds array.
The image is displayed in the On Paint () override:
Finally, note the modification made to the IDE· generated Forml. Dispose () method:
Disposing of the Image as soon as possible when it is no longer needed is important because images generally take up a lot of memory while in use, After image, Dispose () has been called, the Image instance no longer refers to any actual image, and so it can no longer be displayed (unless you load a new image).
Figure 33-14 shows the result of running this code.