If you are implementing GDI+, you are probably planning to do one of two things: Render an existing image or draw your own image. You will cover rendering an existing image first, as it is the easier of the two processes.
Here's the process in a nutshell. Load the image. Draw the image. That's it. And it can be done in one line, too!
g->DrawImageUnscaled(Image::FromFile(S"MCppCover.jpg"), 0.0, 0.0);
Of course, if you want a little more control, there is another DrawImage() method that you can work with. The Image class has a few members (see Table 11-15) with which you can manipulate the image.
MEMBER | DESCRIPTION |
---|---|
FromFile() | Static method to load an image from a file |
FromHbitmap() | Static method to load a bitmap from a Windows handle |
FromStream() | Static method to load an image from a stream |
GetBounds() | Returns a bounding rectangle for the image |
Height | Specifies the height of the image |
HorizontalResolution | Specifies the horizontal resolution of the image in pixels per inch |
PhysicalDimensions | Specifies the size of the image |
RotateFlip() | Rotates, flips, or rotates and flips the image |
Save() | Saves the file to a stream |
Size | Specifies the size of the image |
VerticalResolution | Specifies the vertical resolution of the image in pixels per inch |
Width | Specifies the width of the image |
Before you can render an image, you need to load it from some source, either from a file as shown previously or a data stream (maybe the Internet?). Once the image is loaded, the Image class provides you the ability to flip and rotate the image.
Note | The Image class doesn't use the GraphicsUnit, as you might expect. Instead, it uses pixels per inch. |
Once you have an image, you're ready to render it. You've seen the Graphics class's DrawImageUnscaled() method. That is about the extent of the functionality it provides. It can take an image and the location where you want to place it. A more flexible rendering method is DrawImage(). It takes myriad overloads (you can examine them at your leisure within the .NET Framework documentation), but the most useful overload takes the image and stretches it to the size you want (see Listing 11-14).
Listing 11-14: Stretching an Image
namespace DrawImage { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public __gc class Forml : public System::Windows::Forms::Form { public: Form1(void) //... protected: void Dispose(Boolean disposing) //... private: System::ComponentModel::Container * components; void InitializeComponent(void) { this->AutoScaleBaseSize = System::Drawing::Size(6, 15); this->ClientSize = System::Drawing::Size(292, 265); this->Name = S"Form1"; this->Text = S"Draw Image"; this->Paint += new System::Windows::Forms::PaintEventHandler(this,Form1_Paint); } private: System::Void Form1_Paint(System::Object * sender, System::Windows::Forms::PaintEventArgs * e) { Graphics *g = e->Graphics; Image *img = Image::FromFile(S"MCppCover.jpg"); g->DrawImage(img, 0, 0, img->Width*2, img->Height*2); } }; }
Figure 11-14 shows the end result of DrawImage.exe, which doubles the image with the DrawImage() method. It is a little blurry but not too bad.
Figure 11-14: Doubling an image's size
One last note about rendering images. So far you have only loaded images from files of type .jpg, but you can actually load .bmp, .gif, .png, and .tif image files without having to change a single line of code other than the name of the file.