Click here to Skip to main content
Licence 
First Posted 11 Nov 2005
Views 41,784
Bookmarked 26 times

Folder Resize

By | 11 Nov 2005 | Article
How to resize a large number of images.

Sample Image

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.

private void button1_Click(object sender, System.EventArgs e)
{
    DialogResult dr = folderBrowserDialog1.ShowDialog();
    if(dr == DialogResult.OK)    
    {
        path = folderBrowserDialog1.SelectedPath;
        textBox1.Text=path;
        button2.Enabled=true;
    }

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);
    newDiroctory.Create();

    // 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
            
    progressBar1.Minimum=0;
    progressBar1.Maximum=images.Length;
    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"||
           fileExtention=="JPG"||fileExtention=="BMP"||
           fileExtention=="gif"||fileExtention=="Gif")
        {
            couter++;

            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 = 
                  float.Parse(comboBox1.SelectedItem.ToString());
            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,
                                 images[i].Length-path.Length-5);

            // 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
            if(fileExtention=="bmp"||fileExtention=="BMP")
            newBitmap.Save(newPath+imageName+".bmp",ImageFormat.Bmp);
            if(fileExtention=="JPG"||fileExtention=="jpg")
                newBitmap.Save(newPath+imageName+".jpg",ImageFormat.Jpeg);
            if(fileExtention=="gif"||fileExtention=="GIF")
                newBitmap.Save(newPath+imageName+".gif",ImageFormat.Gif);
            progressBar1.Value++;
        }
    }
    
    MessageBox.Show(couter.ToString()+ 
      " images was resized and its path is:"+newPath,"Done");
}

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

MinaFawzi

Web Developer

Egypt Egypt

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSystem.Drawing not available in VS05eX with 2.0 [modified] Pinmemberm.kruse12:43 26 Jun '06  
GeneralWhy not have filters? GDI is horrible PinmemberKory.Postma2:40 18 Nov '05  
GeneralGDI+ isn't *that* bad PinmemberITGFanatic6:54 30 Oct '06  
Questioncompression or Resize Pinmemberyfoulon2:27 16 Nov '05  
GeneralHi Fawzi Pinmemberr_mendel0:14 15 Nov '05  
GeneralGlobalization Pinmemberyfoulon21:17 14 Nov '05  
if "." is not the separator of the current culture, program crashes while parsing float factor (this was my case in France) ;
 
this can be avoided by modifying call to Parse :
//float factor = float.Parse(comboBox1.SelectedItem.ToString());
float factor = float.Parse(comboBox1.SelectedItem.ToString(), new CultureInfo("en-GB"));
 

And adding these two lines at the end of the loop for each image improves greatly memory usage and speed for many big images resize
 
newBitmap.Dispose();
currentImage.Dispose();

 
yfo
 
-- modified at 5:31 Thursday 1st December, 2005
Generalsee also PinmemberAlberto Venditti21:06 14 Nov '05  
GeneralA fix to memory leaks suggested PinmemberJun Du15:29 11 Nov '05  
GeneralRe: A fix to memory leaks suggested PinprotectorMarc Clifton2:28 13 Nov '05  
GeneralRe: A fix to memory leaks suggested PinmemberMinaFawzi4:49 13 Nov '05  
GeneralRe: A fix to memory leaks suggested PinmemberJun Du5:23 13 Nov '05  
GeneralRe: A fix to memory leaks suggested Pinmemberyfoulon20:57 14 Nov '05  
GeneralRe: A fix to memory leaks suggested PinmemberJun Du12:05 15 Nov '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.

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