Introduction

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.

Background

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 MaxImageLengthproperty.

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 MaxImageLengthproperty. 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.

Examples

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.

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.

I'm not sure, you may want to measure the total amount of memory consumed by the process. Images will be loaded as a bitmap so don't expect the amount to be increased by 70 Mb or something. I'll try to see if I can test with such large images.

In the method that calculates the destination size when using MaintainAspectRatio setting, there is a small bug. If the width of the source image is wider than the height, you need to recalculate the ratio but switch the order of the division, otherwise it will make the thumbnail reverse which dimension it makes larger.

Note how i put the dcRectangle calculation within the if and else statements instead of once at the top. in my testing, if the width of the image was larger than the height, the destination image was ending up with a larger height versus width.

also, not totally sure why you are doing conversion ToString in the two places below and then an int.parse?

The GetThumbnailImage method works well when the requested thumbnail image has a size of about 120 x 120 pixels. If you request a large thumbnail image (for example, 300 x 300) from an Image that has an embedded thumbnail, there could be a noticeable loss of quality in the thumbnail image. It might be better to scale the main image (instead of scaling the embedded thumbnail) by calling the DrawImage method.