C#
  Home arrow C# arrow Page 2 - Thumbnails and Zooming with GDI+ and C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Thumbnails and Zooming with GDI+ and C#
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2006-10-31

    Table of Contents:
  • Thumbnails and Zooming with GDI+ and C#
  • Image Manipulation: Thumbnail, Zooming and Saving
  • Image Manipulation in the Real World
  • Image Manipulation in the Real World, continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    1. Creating thumbnails.
    2. Zooming a loaded image.
    3. 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.

    More C# Articles
    More By A.P.Rajshekhar


       · In the second part of the series I have discussed about the working of GDI+ as well...
     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT