Folder Resize

Introduction

When I downloaded my pictures from my digital camera I found that they were very large and I had to spend a long time to upload them and to resize them individually. So I wrote this simple application that resizes all the images in a specified folder.

Using the code

The code is very simple. As you can see in the form, it is very simple and understandable. First you choose the folder and then click Start to begin the resizing process and you can change the resizing factor and the folder name that will contain the resized images.

The code uses the System.IO and System.Drawing.Imaging namespaces. The first one is used to create folders and the second one used to specify the image format.

The first button will open the FolderBrowserDialog which allows you to choose the folder that contains the images to be resized.

Comments and Discussions

I am new to C# and started coding some little tools. I wanted to play around with this piece of code, but Visual Studio 2005 Express says: "The Namespace System.Drawing doesnt exists"?
Do I have to use another Namespace with .NET 2.0 or where's the mistake?

I have written a utility of my own to resize tons of images (18,000+ in one fell swoop). I use a Lanscoz 3 Filter (spelling?) and it gives much much better results than GDI's crappy filters (I think they use a nearest neighbor or Box filter). They always come out so blocky. I have a little program to show the difference if anyone is interested. Just email me: kory@mediapitstop.net

There are memory leaks though. Basically, I have a photo folder containing some 900 photos of 1280x960 pixels. I tried to resize all of them to 10% of the original size (I added 0.1 to "resize factor"). But it qickly used up all virtual memory (1.8G) and crashed due to the "out of memory" error. The code appears to be fine, but automatical Garbage Collection does not work properly here.

One fix to this is to force a GC manually. To do so, you insert two lines of code to the very begining of the IF loop of image saving:
if()
{
GC.Collect();
GC.WaitForPendingFinalizers();
couter++;
...
}

Note that the GC wait call is necessary for the GC to actually finish the memory-freeing job. There is a cost to this fix, which is performance penalty, i.e., increased CPU usage. I guess it's not as critical as crashing in this case.

This is a problem I discuss here.[^]. I believe, if you place a "currentImage.Dispose()" call at the end of the if section, you won't run out of memory.

Nice utility, BTW. It would be cool if you could also convert image types at the same time (like, jpg to png) and also be able to set the compression algorithm. For example, doesn't JPG support different compression modes? That way, you can choose both image size and compression as factors that affect the resulting file size.

Tried the source code "As is" with .jpg and .bmp ; didn't notice any memory leak but only temporary increase of memory usage (waiting for GC to work !). You can avoid this increase by adding for each image