Stephen A. Fuqua (SAF) is a Bahá'í, software developer, and conservation and interfaith advocate in the DFW area of Texas.

Image Conversion Made Easy in .Net

June 20, 2007

Problem: Need to convert an image from one format to another using .Net.

Solution: Microsoft has really made this easy. The key is the System.Drawing.Image class, combined with the System.Drawing.Imaging.ImageFormat class.

Steps:

  1. In your project's References, add a reference to System.Drawing.
  2. Add System.Drawing and System.Drawing.Imaging to the namespaces list in the class you're editing (System.Drawing may be added automatically by step 1).
  3. If you have a byte stream (i.e. from a web service or a database query result), then you'll need to save it as a file temporarily. Use System.IO.FileStream to write the data out.
  4. Open your input file (I'm converting from a JPG): Image input = Image.FromFile(filePathAndName + ".jpg");
  5. Save the file with the proper format, i.e. as a TIFF: input.Save(filePath + ".tif", ImageFormat.Tiff);

And it's really that simple.