http://www.developer.com/

Back to article

Convert Between Image Formats in .NET


December 22, 2003

Need a function to convert between bitmap, GIF, EMF, JPEG, PNG, WMF, and ICO image formats, among others? Don't buy a third-party control. This conversion is exactly what my next crafty little snippet does. And all in a mere dozen lines of code.

Just call ConvertImage, passing in the filename of your current file, the desired format of your new file (using the enumeration), and your new filename. And that's it:

Public Sub ConvertImage(ByVal Filename As String, _
  ByVal DesiredFormat As System.Drawing.Imaging.ImageFormat, _
  ByVal NewFilename As String)
  ' Takes a filename and saves the file in a new format
  Try
    Dim imgFile As System.Drawing.Image = _
      System.Drawing.Image.FromFile(Filename)
    imgFile.Save(NewFilename, DesiredFormat)
  Catch ex As Exception
    Throw ex
  End Try
End Sub

Here's an example of using this to convert a GIF image into a Windows bitmap:

ConvertImage("c:\img1.gif", _
   System.Drawing.Imaging.ImageFormat.Bmp, _
   "c:\img2.bmp")

About the Author

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is the author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.

# # #

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date