How to Convert Image File from One format to another using vb.net

 
In this article we will discuss about how we can convert an image file from one image format to another like .jpg to .gif or .jpg to .png using vb.net.

We use the Image.save method of System.Drawing.Image class for converting image to another format.
you can use this function as :
Public Sub Save(ByVal filename As String, ByVal format As System.Drawing.Imaging.ImageFormat)

Parameters:
filename: A string that contains the name of the file to which to save this System.Drawing.Image.
format: The System.Drawing.Imaging.ImageFormat for this System.Drawing.Image.

Exceptions:
System.ArgumentNullException: filename or format is null.
System.Runtime.InteropServices.ExternalException: The image was saved with the wrong image format.-or- The image was saved to the same file it was created from.

Example

This example requires a windows form that has some controls like this:

Completer Code :

Imports System.Windows.Forms
Public Class Form2
    Dim SourceFilePath As String = ""
    Dim DestinationFilePath As String = ""
    Private Sub btnSelectSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectSource.Click
        Dim FileDlg As New OpenFileDialog
        If FileDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            SourceFilePath = FileDlg.FileName
            TextBox1.Text = SourceFilePath
        End If
    End Sub
 
    Private Sub btnSelectDes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectDes.Click
        Dim folderDlg As New FolderBrowserDialog
        If folderDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            DestinationFilePath = folderDlg.SelectedPath
            TextBox2.Text = DestinationFilePath
        End If
    End Sub
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbImageFormat.Items.Add("Bmp")
        cmbImageFormat.Items.Add("Jpeg")
        cmbImageFormat.Items.Add("Png")
        cmbImageFormat.Items.Add("Gif")
        cmbImageFormat.Items.Add("Icon")
    End Sub
 
    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        If SourceFilePath.Trim = "" Then
            MessageBox.Show("Please enter source file path")
            Return
        End If
        If DestinationFilePath.Trim = "" Then
            MessageBox.Show("Please enter destination file path")
            Return
        End If
        Dim DesFilepath As String = DestinationFilePath & "\" & txtFilename.Text
        Dim strFormat As String = cmbImageFormat.Text
        Dim imgFormat As Imaging.ImageFormat
        Select Case strFormat
            Case "Bmp"
                imgFormat = Imaging.ImageFormat.Bmp
                DesFilepath = DesFilepath & ".bmp"
            Case "Jpeg"
                imgFormat = Imaging.ImageFormat.Jpeg
                DesFilepath = DesFilepath & ".jpeg"
            Case "Png"
                imgFormat = Imaging.ImageFormat.Png
                DesFilepath = DesFilepath & ".png"
            Case "Gif"
                imgFormat = Imaging.ImageFormat.Gif
                DesFilepath = DesFilepath & ".gif"
            Case "Icon"
                imgFormat = Imaging.ImageFormat.Icon
                DesFilepath = DesFilepath & ".icon"
        End Select
 
        Dim SourceImg As Image
        SourceImg = Image.FromFile(SourceFilePath)
        SourceImg.Save(DesFilepath, imgFormat)
        MessageBox.Show("Convert image successfully")
    End Sub
End Class