Click here to Skip to main content
13,593,290 members
Click here to Skip to main content
Add your own
alternative version


58 bookmarked
Posted 6 Mar 2009
Licenced CPOL

Thumbnails Generation

, 6 Mar 2009
Rate this:
Please Sign up or sign in to vote.
A generic way for generating thumbnails


I started as a developer several years ago creating websites. Now I'm working for a company which creates websites, but also multimedia applications. Each time we start developing a new solution, there's this need for image processing. In case of a website we need to generate thumbnails, and for our Windows applications images are restricted to a certain size. To accomplish thumbnails creation and image resizing, we use standard .NET Framework functions. However I was sick and tired of developing the same code over and over again with a slight difference required for each solution. Therefore I decided to develop an image processing class which is able to process images in a more dynamic way. 


Basically the class is pretty simple. It resizes images and stores them again (or returns the image as an object if you like). Depending on your needs, you're able to set the image's size, quality, resizing method and so forth.

Using the Code

The ThumbnailCreation class exposes four properties:

  • ScalingOption
  • MaxImageLength
  • FileFormat
  • Quality

ScalingOption (read-only) Property

The scaling option enables you to tell the ThumbnailCreation class to scale the image in different ways. There are three options available:

  • MaintainAspect
  • CenterImage
  • FixedSize

You need to set the ScalingOption using a method called SetScalingOption(), which forces you to also define a size when scaling option FixedSize is used.
The default is called MaintainAspect which will maintain the aspect ratio and resize the image so that the largest image side (width or height) equals the value set using the MaxImageLength property.


When the ScalingOption is set to CenterImage, the center of the image will be scaled so that the largest side (width of height) of the image equals the value set using the MaxImageLength property. The center of the image is a square of half the width and half the height of an image. The image below shows you the result of a thumbnail created using the CenterImage scaling option.


The third scaling option is FixedSize. You need to define a size and your source image will be scaled to that size.

MaxImageLength Property

The MaxImageLength property is used to define the size of thumbnails. This property is only used for the scaling options MaintainAspect and CenterImage. The largest side of the image (width or height) will equal this value in pixels. The remaining side is scaled so that the aspect ratio of the image will be maintained.

FileFormat Property

The FileFormat property enables you to define whether images will be stored as JPEG or PNG image. The default is JPEG. This option is added because you may want to use the PNG format for transparency.


And now... a few examples of calling the object's StoreThumbnail (which actually stores a thumbnail version of your image on the harddrive). The next example shows you the easiest way of generating a thumbnail accepting all default values.

using (ThumbnailCreation objThumbnailCreator = new ThumbnailCreation())
    objThumbnailCreator.StoreThumbnail(SourceFile, DestinationFile);		

The example below shows the code for generating a thumbnail having the ScalingOption property set to CenterImage. The default thumbnail size of 120 pixels is changed to 150 pixels and the image's format will be PNG.

using (ThumbnailCreation objThumbnailCreator = new ThumbnailCreation())
    objThumbnailCreator.MaxImageLength = 150; // Max image side to 150 pixels
    objThumbnailCreator.Quality = 100; 	// Best quality
    objThumbnailCreator.FileFormat = FileFormats.PNG;
    objThumbnailCreator.StoreThumbnail(SourceFile, DestinationFile);


  • 2009/03/06 - Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Eduard Keilholz
Software Developer (Senior)
Netherlands Netherlands
In 1998 I started as webdesigner programming websites in Perl and later PHP. After two years wrote most of the websites in ASP and from then on lost the feeling with a linux/unix platform.

Since 2001 interested in Windows applications and now writing software using mostly C# for about 7 years now.

You may also be interested in...

Comments and Discussions

QuestionOut of Memory Exception Pin
Peter Freestone6-Mar-13 10:54
memberPeter Freestone6-Mar-13 10:54 
AnswerRe: Out of Memory Exception Pin
Eduard Keilholz9-Mar-13 9:28
memberEduard Keilholz9-Mar-13 9:28 
Suggestionratio calculation bug fix Pin
Kirk Quinbar22-Mar-12 11:31
memberKirk Quinbar22-Mar-12 11:31 
GeneralMy vote of 5 Pin
aalhanane10-Jun-11 8:22
memberaalhanane10-Jun-11 8:22 
GeneralMy vote of 5 Pin
edjeit18-Mar-11 4:02
memberedjeit18-Mar-11 4:02 
GeneralThank you! Pin
mhoward007-Dec-09 9:36
membermhoward007-Dec-09 9:36 
So helpful, thank you!
GeneralDetermineSizeByRatio Pin
lGiles19-May-09 10:34
memberlGiles19-May-09 10:34 
GeneralRe: DetermineSizeByRatio Pin
Eduard Keilholz19-May-09 21:28
memberEduard Keilholz19-May-09 21:28 
GeneralRe: DetermineSizeByRatio Pin
lGiles20-May-09 5:47
memberlGiles20-May-09 5:47 
GeneralRe: DetermineSizeByRatio Pin
Eduard Keilholz20-May-09 10:54
memberEduard Keilholz20-May-09 10:54 
GeneralRe: DetermineSizeByRatio Pin
lGiles20-May-09 11:04
memberlGiles20-May-09 11:04 
GeneralRe: DetermineSizeByRatio Pin
Eduard Keilholz25-May-09 21:15
memberEduard Keilholz25-May-09 21:15 
GeneralRe: DetermineSizeByRatio Pin
lGiles26-May-09 8:09
memberlGiles26-May-09 8:09 
GeneralRe: DetermineSizeByRatio Pin
Eduard Keilholz26-May-09 23:55
memberEduard Keilholz26-May-09 23:55 
GeneralNice Artikle, thank you. Pin
Hardy_Smith19-Mar-09 4:25
memberHardy_Smith19-Mar-09 4:25 
GeneralRe: Nice Artikle, thank you. Pin
Eduard Keilholz19-Mar-09 6:55
memberEduard Keilholz19-Mar-09 6:55 
GeneralRe: Nice Artikle, thank you. Pin
Hardy_Smith19-Mar-09 7:31
memberHardy_Smith19-Mar-09 7:31 
GeneralRe: Nice Artikle, thank you. Pin
Eduard Keilholz19-Mar-09 10:14
memberEduard Keilholz19-Mar-09 10:14 
GeneralRe: Nice Artikle, thank you. Pin
lGiles26-May-09 8:22
memberlGiles26-May-09 8:22 
GeneralRe: Nice Artikle, thank you. Pin
Hardy_Smith9-Oct-09 23:22
memberHardy_Smith9-Oct-09 23:22 
GeneralGreat Stuff! Pin
Sean J Conway17-Mar-09 17:44
memberSean J Conway17-Mar-09 17:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.180618.1 | Last Updated 6 Mar 2009
Article Copyright 2009 by Eduard Keilholz
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid