Resize all of the pictures in a directory in C#
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" (for "small") added to the names.
The most interesting work occurs when you click the Go button. The code first gets the scale factor and sets the form's cursor to a wait cursor.
Next the program gets a DirectoryInfo object for the directory you entered in the TextBox and loops through the directory's files. If the 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 program makes a Rectangle covering the original bitmap. It then makes 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. It draws the original image onto the Graphics object, using the original and scaled Rectangles to resize the image.
Finally the program composes a new file name and saves the file in an appropriate format.
// 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, MessageBoxIcon.Error);
return;
}
this.Cursor = Cursors.WaitCursor;
this.Refresh();
DirectoryInfo dir_info = new System.IO.DirectoryInfo(txtDirectory.Text);
foreach (FileInfo file_info in dir_info.GetFiles())
{
try
{
string ext = file_info.Extension.ToLower();
if ((ext == ".bmp") || (ext == ".gif") ||
(ext == ".jpg") || (ext == ".jpeg") ||
(ext == ".png"))
{
Bitmap bm = new Bitmap(file_info.FullName);
picWorking.Image = bm;
this.Text = "howto_2005_resize_pics - " + file_info.Name;
Application.DoEvents();
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);
Bitmap bm2 = new Bitmap(wid2, hgt2);
Rectangle dest_rect = new Rectangle(0, 0, wid2, hgt2);
using (Graphics gr = Graphics.FromImage(bm2))
{
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
gr.DrawImage(bm, dest_rect, from_rect, GraphicsUnit.Pixel);
}
string new_name = file_info.FullName;
new_name = new_name.Substring(0, new_name.Length - ext.Length);
new_name += "s" + ext;
switch (ext)
{
case ".bmp":
bm2.Save(new_name, System.Drawing.Imaging.ImageFormat.Bmp);
break;
case ".gif":
bm2.Save(new_name, System.Drawing.Imaging.ImageFormat.Gif);
break;
case ".jpg":
case ".jpeg":
bm2.Save(new_name, System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case ".png":
bm2.Save(new_name, System.Drawing.Imaging.ImageFormat.Png);
break;
}
} // if it's a graphic extension
}
catch (Exception ex)
{
MessageBox.Show("Error processing file '" +
file_info.Name + "'\n" + ex.Message,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
} // foreach file_info
picWorking.Image = null;
this.Text = "howto_2005_resize_pics";
this.Cursor = Cursors.Default;
}



Comments