Change image file types in a directory in C#

Change image file types

This program lets you save the images in a directory with new image file types. Enter a directory name and select a graphical file type: bmp, jpg, gif, png, etc. When you click Go, the proigram loads each of the image files in the directory and re-saves the image in the selected file format. (Note that it doesn’t delete the old image files. You could add that to the program, or you could make the program move the old files into a new directory.)

The following code shows the most interesting part of the program, which executes when you click the Go button.

// Process the files in the selected directory.
private void btnGo_Click(object sender, EventArgs e)
{
    this.Cursor = Cursors.WaitCursor;
    this.Refresh();

    // Get the new image format.
    string new_ext = cboExtension.Text;
    ImageFormat new_format = ExtensionFormat(new_ext);

    // Enumerate the files.
    DirectoryInfo dir_info = new DirectoryInfo(txtDirectory.Text);
    foreach (FileInfo file_info in dir_info.GetFiles())
    {
        try
        {
            txtProcessing.Text = file_info.Name;
            txtProcessing.Refresh();

            // See what kind of file this is.
            string old_ext = file_info.Extension.ToLower();
            ImageFormat old_format = ExtensionFormat(old_ext);

            // Only process if the file has a graphic
            // extension and we're changing the type
            if ((old_format != null) && (old_format != new_format))
            {
                Bitmap bm = new Bitmap(file_info.FullName);
                string new_name =
                    file_info.FullName.Replace(old_ext, new_ext);

                bm.Save(new_name, new_format);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error processing file '" +
                file_info.Name + "'\n" + ex.Message,
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }
    } // foreach file_info

    txtProcessing.Clear();
    this.Text = "howto_2005_change_picture_types";
    this.Cursor = Cursors.Default;
}

The program uses the ExtensionFormat function to determine the image format for the selected extension. For example, the extension .bmp corresponds to the image format ImageFormat.Bmp. The ExtensionFormat function is basically a straightforward switch statement so it isn’t shown here.

Next the program gets a DirectoryInfo object for the directory you entered in the text box and uses it to loop through the directory’s files.

For each file, the program gets the file’s image format. If the file’s format is not null (indicating that it is a graphic format) and is different from the desired new format, the program loads the file into a Bitmap and then saves it in the new format.


Download Example   Follow me on Twitter   RSS feed


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

Leave a Reply

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