Crafty Conversion Between Graphic Formats

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
        Dim imgFile As System.Drawing.Image = _
        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")

You might also like...


Karl Moore


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”