SAF(NET) = STEPHEN A. FUQUA operating on the Web since 1995

Stephen is a web developer, Bahá'í, and interfaith activist in St. Paul, Minnesota. He likes to write about religion, social justice, sustainability, science, programming, &c.

June 20, 2007

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.

Comments

Post a comment

Remember personal info?




deprecated

On safnet.com

Other sites managed or developed by S.A.F.

S.A.F. elsewhere on the web

  • LinkedIn
    LinkedIn can actually be useful when looking for prospective hires and business or organizational partners
  • GoodReads
    A fun and relatively-unknown social networking site geared towards one's book list
  • Live Journal
    Mirror of the blog at safnet.com, so that a few LJ friends can more easily read and comment there