[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: 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 program 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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.