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:
- In your project's References, add a reference to System.Drawing.
- 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).
- 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.
- Open your input file (I'm converting from a JPG):
Image input = Image.FromFile(filePathAndName + ".jpg");
- Save the file with the proper format, i.e. as a TIFF:
input.Save(filePath + ".tif", ImageFormat.Tiff);
And it's really that simple.