Stephen A. Fuqua (saf)

a Bahá'í, software engineer, and nature lover in Austin, Texas, USA

Image Conversion Made Easy in .Net

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.

Posted with : Tech, Microsoft .NET Framework