High quality thumbnails in VB.Net

When i started .Net programming i appreciated all the new functionalities of the framework, everything was easier and faster to do. I tought the same thing about thumbnails generation when i found the GetThumbnailImage method of the System.Drawing.Bitmap class but giving a deeper look at it’s behaviour i understood it was not so easy.

WHY IT WASN’T SO EASY
The GetThumbnailImage method isn’t so useful because image formats like JPG can store the thumbnail image inside the same file. A lot of graphics softwares store the thumbnail version of the image into the .JPG file and you can’t choose it’s width, height and quality. The GetThumbnailImage method checks if there’s a thumbnail image stored into the file and, if the thumb is found, it returns that thumbnail version scaled to the width and height you requested. If the thumbnail version of the image is smaller then the size you asked for you can imagine what will be the result, a very low quality and pixelated image. If you want to have the full control over the thumbnail quality you have to use something different by the GetThumbnailImage method.

THE APPROACH
What we have to do is to load the JPG file into a System.Drawing.Image object, scale it to the desired width and height preserving a good quality and save it to a new file.

To preserve the image quality we have to follow these few steps:

  • Load the original file into a System.Drawing.Image object
  • Create the target image with the desired size as System.Drawing.Bitmap
  • Create a System.Drawing.Graphics object from the target Bitmap to draw the scaled image
  • Set the System.Drawing.Graphics object property SmoothingMode to HighQuality
  • Set the System.Drawing.Graphics object property CompositingQuality to HighQuality
  • Set the System.Drawing.Graphics object property InterpolationMode to High
  • Draw the original image into the target Graphics object scaling to the desired width and height
  • Get the System.Drawing.Imaging.ImageCodecInfo object of the JPEG file format
  • Set the Codec param System.Drawing.Imaging.Encoder.Quality to the desired quality (for jpg is 0-100)
  • Save the new image using the Codec and the filled quality parameter

THE CODE
I wrote a simple class CImageHelper where i put all the imaging routines, here there’s a simple version with the SaveImage function to save ThumbNails (or scaled versions of any image) with a very high quality.

Click here to download the code


TAGS: Asp.net, dotnet, code snippet, seo, search engine optimization, visual studio.net, sample code, c#, vb.net

1 Comment so far »

  1. Nicola said

    am November 16 2007 @ 10:17 pm

    Since if found your routine i have been using it in many of my projects. Thank you very much!!! I am happy you shared your code, but i’m still having a problem regarding exif data of the image, as they are deleted during thumbnail creation.
    I am not using the code to create thumbnails but to reduce image dimension once uploaded to a server, so i’d like exif data to be at their place in order to read them afterwards.
    Any way to get rid of the problem?

Comment RSS · TrackBack URI

Leave a comment

Name:

eMail:

Website:

Comment: