Thumbnails and Zooming with GDI+ and C# - Image Manipulation: Thumbnail, Zooming and Saving
(Page 2 of 4 )
In the last part, I discussed simple manipulations such as flipping and rotating. Lets tackle some topics that are a bit more complex in concept if not in implementation. They are:
- Creating thumbnails.
- Zooming a loaded image.
- Saving a manipulated image.
Of these the first two come under the category of image manipulation (strictly speaking) whereas the last is a generic image-based task along the lines of a file operation.
Creating Thumbnails
Thumbnails are reduced versions of images. The dimensions of a thumbnail image are typically of 80 to 200 pixels in length. In GDI+, a thumbnail of an image can be created by GetThumbnailImage() of Image class. It takes four parameters.
The first parameter corresponds to the width. The second parameter is for the height of the thumbnail to be generated. The third parameter is Image.GetThumbnailImageAbort, which needs to be passed for compatibility, though it is not being used in the current version. The fourth parameter is likewise not used, but needs to be passed for compatibility. The fourth parameter must be IntPtr.Zero.
If the first two parameters, namely width and height, are 0, then GDI+ returns an embedded thumbnail (some images contain thumbnails embedded into them). Otherwise the thumbnail is created using system-defined dimensions. For example if img is an instance of the Image class and the width and height to be used are system defined, the statement to create a thumbnail would be:
Image thumbNailImage = img.GetThumbnailImage(0, 0,tnCallBack,
IntPtr.Zero);
where thumbNailImage contains the returned thumbnail, and the tnCallback is a function corresponding to Image.GetThumbnailImageAbort which is defined as:
// Must be called, but not used
style='font-size:10.0pt;font-family:Verdana'>public bool tnCallbackMethod()
{
return false;
}
Zooming a loaded image
Zooming is the process of enlarging an image by multiplying it with a number called the zoom factor. By definition, “The zoom factor is the ratio of the current size of the image to the desired new size of the image.” By dividing the current size of the image with the desired size of the image, we get the zoom factor. For example, to zoom in on an image by 200%, the current size must be multiplied by 200% or by 2 (200%= 200/100=2). To zoom out an image by 25 percent, the size must be multiplied by 25 percent, or 0.25 (25/100 = 0.25 times). It is one of those areas of image manipulation where GDI+ doesn’t provide methods to achieve the result.
Saving a manipulated image
Saving is one of the primary operations done on an image. While saving an image, the type into which the image has to be saved, or in other words the extension of the image, plays a major role. Each type corresponds to a particular format. In essence, while saving an image, writing out the data according to the format is necessary. However, since the API that is being used is GDI+, a single call to the Save() method of the Image class abstracts out all the details of writing out the data according to the format. It takes two parameters, the name as which the image is to be saved and the format into which the image has to be saved. The format can be specified using the types provided by the ImageFormat class. The following table specifies the various formats supported by GDI+.
Property | Description |
Bmp | Specifies BMP format. |
Emf | Specifies EMF (Enhanced Metafile Format). |
Exif | Specifies EXIF format. |
Gif | Specifies GIF format. |
Guid | Specifies a GUID structure that represents the ImageFormatobject. |
Icon | Specifies Windows icon format. |
Jpeg | Specifies JPEG format. |
MemoryBmp | Specifies memory bitmap format. |
Png | Specifies PNG format. |
Tiff | Specifies TIFF format. |
Wmf | Specifies WMF (Windows Metafile Format). |
Of these, Emf and Wmf are specific to Windows. I will be discussing these in detail in the future.
To give you an example, if you wanted to save an image with the name “checker.gif” in GIF format, the statement would be:
curImage.Save(“checker.gif”, ImageFormat.Gif);
where curImage is the instance of the Image class.
That brings us to the end of this section. In the next section, I will be extending the application developed in the first part by embedding the operations discussed in this section.
Next: Image Manipulation in the Real World >>
More C# Articles
More By A.P.Rajshekhar