Click here to Skip to main content
11,796,774 members (62,653 online)
Click here to Skip to main content

Folder Resize

, 11 Nov 2005 CPOL 48.6K 612 27
Rate this:
Please Sign up or sign in to vote.
How to resize a large number of images.

Sample Image


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.

private void button1_Click(object sender, System.EventArgs e)
    DialogResult dr = folderBrowserDialog1.ShowDialog();
    if(dr == DialogResult.OK)    
        path = folderBrowserDialog1.SelectedPath;

After we choose the folder we click on the second button to start resizing and here is the code:

private void button2_Click(object sender, System.EventArgs e)
    // Create The new directory with the name specified in the textbox

    DirectoryInfo newDiroctory = new DirectoryInfo(path+"\\"+textBox3.Text);

    // This is the new path that will contain the new resized images
    string newPath =path +"\\"+ textBox3.Text+"\\"; 

    string []images = Directory.GetFiles(path);
    // this array contain ALL files in the specified directory
    int couter = 0; // this will count the number of images in the folder
    for (int i=0;i < images.Length;i++)

        // select only the image format in the folder
        string fileExtention = 
               images[i].Substring(images[i].Length - 3, 3);
        if(fileExtention== "bmp"||fileExtention=="jpg"||

            Image currentImage = Image.FromFile(images[i]);
            int h = currentImage.Height;
            int w = currentImage.Width;

            // calculate the new dimensions
            // according to the resizing factor
            float factor = 
            int newH = (int)Math.Ceiling((double)h*factor);
            int newW = (int)Math.Ceiling((double)w*factor);
            // get the Image name from the path 
            string imageName = images[i].Substring(path.Length+1,

            // create the new bitmap with the specified size
            Bitmap newBitmap = new Bitmap(currentImage,new Size(newW,newH));
            // according to type of the file we will save the new image
      " images was resized and its path is:"+newPath,"Done");

The code is very simple and there is a lot of helpful comments.


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


About the Author

Web Developer
Egypt Egypt
Mina Fawzi
Faculty of engineering Ainshams university In CAIRO
intersted in .net technology and DirectX

You may also be interested in...

Comments and Discussions

GeneralSystem.Drawing not available in VS05eX with 2.0 [modified] Pin
m.kruse26-Jun-06 12:43
memberm.kruse26-Jun-06 12:43 
GeneralWhy not have filters? GDI is horrible Pin
Kory.Postma18-Nov-05 2:40
memberKory.Postma18-Nov-05 2:40 
GeneralGDI+ isn't *that* bad Pin
ITGFanatic30-Oct-06 6:54
memberITGFanatic30-Oct-06 6:54 
Questioncompression or Resize Pin
yfoulon16-Nov-05 2:27
memberyfoulon16-Nov-05 2:27 
GeneralHi Fawzi Pin
r_mendel15-Nov-05 0:14
memberr_mendel15-Nov-05 0:14 
GeneralGlobalization Pin
yfoulon14-Nov-05 21:17
memberyfoulon14-Nov-05 21:17 
Generalsee also Pin
Alberto Venditti14-Nov-05 21:06
memberAlberto Venditti14-Nov-05 21:06 
GeneralA fix to memory leaks suggested Pin
Jun Du11-Nov-05 15:29
memberJun Du11-Nov-05 15:29 
A useful little tool and I like to use it.

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:

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.

Happy codingSmile | :)

GeneralRe: A fix to memory leaks suggested Pin
Marc Clifton13-Nov-05 2:28
protectorMarc Clifton13-Nov-05 2:28 
GeneralRe: A fix to memory leaks suggested Pin
MinaFawzi13-Nov-05 4:49
memberMinaFawzi13-Nov-05 4:49 
GeneralRe: A fix to memory leaks suggested Pin
Jun Du13-Nov-05 5:23
memberJun Du13-Nov-05 5:23 
GeneralRe: A fix to memory leaks suggested Pin
yfoulon14-Nov-05 20:57
memberyfoulon14-Nov-05 20:57 
GeneralRe: A fix to memory leaks suggested Pin
Jun Du15-Nov-05 12:05
memberJun Du15-Nov-05 12:05 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151002.1 | Last Updated 11 Nov 2005
Article Copyright 2005 by MinaFawzi
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid