Resize pictures in a directory in C#


This example shows how to resize pictures in a directory.

This program lets you enter a directory name and a scale factor. It then loads all of the bmp, gif, jpg, jpeg, and png files in that directory, resizes them, and saves the resized versions back into the directory with an “_s” added to their names. (I wrote this program to make smaller versions of a bunch of pictures I had taken, so the “_s” stands for “small.”)

The most interesting work occurs in the following event handler when you click the Go button.

// Process the files in the selected directory.
private void btnGo_Click(object sender, EventArgs e)
    float scale = float.Parse(txtScale.Text);
    if (scale == 0)
        MessageBox.Show("Scale must not be zero.",
            "Invalid Scale", MessageBoxButtons.OK,
    Cursor = Cursors.WaitCursor;

    DirectoryInfo dir_info = new DirectoryInfo(txtDirectory.Text);
    foreach (FileInfo file_info in dir_info.GetFiles())
            string ext = file_info.Extension.ToLower();
            if ((ext == ".bmp") || (ext == ".gif") ||
                (ext == ".jpg") || (ext == ".jpeg") ||
                (ext == ".png"))
                using (Bitmap bm = new Bitmap(file_info.FullName))
                    picWorking.Image = bm;
                    Text = "howto_2005_resize_pics - " +

                    Rectangle from_rect =
                        new Rectangle(0, 0, bm.Width, bm.Height);

                    int wid2 = (int)Math.Round(scale * bm.Width);
                    int hgt2 = (int)Math.Round(scale * bm.Height);
                    using (Bitmap bm2 = new Bitmap(wid2, hgt2))
                        Rectangle dest_rect =
                            new Rectangle(0, 0, wid2, hgt2);
                        using (Graphics gr =
                            gr.InterpolationMode =
                            gr.DrawImage(bm, dest_rect, from_rect,

                        string new_name = file_info.FullName;
                        new_name = new_name.Substring(0,
                            new_name.Length - ext.Length);
                        new_name += "_s" + ext;
                        SaveImage(bm2, new_name);

                        picWorking.Image = null;
            } // if it's a graphic extension
        catch (Exception ex)
            MessageBox.Show("Error processing file '" +
                file_info.Name + "'\n" + ex.Message,
    } // foreach file_info

    Text = "howto_resize_pics";
    Cursor = Cursors.Default;

The code first gets the scale factor and displays the wait cursor. It then gets a DirectoryInfo object for the directory you entered in the text box and uses it to loop through the directory’s files. If a file’s extension is .bmp, .gif, .jpg, .jpeg, or .png (you can add others if you like), it processes that file.

The code loads the file into a Bitmap. It displays the bitmap and its name so you can see what it is doing. The images may go too fast to really see but at least you can tell something’s happening.

The program makes a Rectangle covering the original bitmap. It then creates a new scaled Bitmap and makes a Rectangle to fit it.

Next the code creates a Graphics object associated with the new Bitmap. It sets the Graphics object’s InterpolationMode property to HighQualityBicubic so the image is resized smoothly. The code draws the original image onto the new Graphics object, using the original Bitmap and the Rectangles to resize the image.

Finally the program composes a new file name and saves the file in an appropriate format. (See the example Save images with an appropriate format depending on the file name’s extension in C# for information about the SaveImage method.)

Download Example

This entry was posted in files, graphics and tagged , , , , , , , , , , , , , , , . Bookmark the permalink.

4 Responses to Resize pictures in a directory in C#

  1. Ello says:

    Another way to get _s into the name or change it to any value you want

    //Get index of period and insert _s there.
    string newName = file.Insert(file.IndexOf('.'), "_s");  
    public string NameOfNewFiles(string input)
            return file.Insert(file.IndexOf('.'), input);           
  2. Penney says:

    How do I convert the scale from 0.25 to pixels? Or how do I change this to 128 x 128 for example?

    • RodStephens says:

      In reply to Alan.
      That example resizes the images by a scale factor. Initially the factor is 0.25 but you can change it at runtime.

      It uses this code to calculate the new size for an image.

      int wid2 = (int)Math.Round(scale * bm.Width);
      int hgt2 = (int)Math.Round(scale * bm.Height);

      You can set wid2 and hgt2 to any values you like. For example, the following would make the new image be 128 x 128 pixels.

      wid2 = 128;
      hgt2 = 128;
  3. Pingback: Resize pictures in a directory to specific widths or heights in C# - C# HelperC# Helper

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.