Saturday, October 18, 2003 4:01 PM bart

Simple Image Convertor

Do you need to convert large amounts of files from one format to another? I have to do it for my articles on So what? Well, I don't like to do the same action all over again. Here's the classic way about how to do this:

  1. Windows-key + R, mspaint, OK
  2. CTRL-O, file1.bmp,
  3. ALT-F, A, file1.jpg, , J,
  4. Resume with step 2 till all the files in the folder are converted

I'm a developer and computers are great to do the same work all over again without complaining. Do you know secretaries? The are suited for such a job as well, but they will probably start to complain about their job... So, I'm a developer and what developers do is this: I have 10 minutes to complete this boring job, so I'm writing for 9 minutes a program which can do the job for me in less than 1 minute (and the development is not boring but exciting).

Let's give you the most important part of the code:

string dir = Directory.GetCurrentDirectory();
foreach(string i in Directory.GetFiles(dir,"*.bmp"))
FileInfo info = new FileInfo(i);
     Console.WriteLine("Processing {0}",info.Name);

     Image img = Image.FromFile(info.FullName);
     string name = info.Name.Substring(0,info.Name.Length - info.Extension.Length);
     img.Save(dir + "\\" + name + ".jpg",ImageFormat.Jpeg);

I made this code a little better using a console application which takes some parameters for the original and destination file format. Take a look at System.Drawing and System.Drawing.Imaging. These are wonderful namespaces! | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:


No Comments