Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Batch Image Format Converter

0.00/5 (No votes)
4 Jun 2011 1  
Convert pictures of different format to desired format, i.e., picture of BMP, JPG, PNG, etc. can be converted into selected one (for say tiff)

Introduction

This article guides you to create an application that can convert images of different formats to the one that you desire, just by selecting it from the combobox. That is, images of different formats such as BMP, JPG, GIF, PNG, etc. can be converted to a format which is either BMP or JPG or GIF as per your requirement at the same time. With the help of this article, you will able to convert images of one format to another without disturbing the other attributes of the image. To do so, drag all images from the explorer that you want to convert, then drop them to the left box. It will accept only the image file, if you drag and drop another file then it won't accept it. Similarly, if it is already accepted, then it will not accept it again.

Using the Code

Following is the main source code:

The below subroutine converts images of one format to other:

Private Sub ImageFormat(ByVal strr As String)

        Dim bm As New Bitmap(strr)
        Dim fileinfo As Image = Image.FromFile(strr)
        Dim i As Integer

        Dim bmname As String = ""
        Dim c As Char = Nothing

        For i = 4 To Len(strr)
            c = Mid(strr, Len(strr) - i, 1)
            If c = Char.Parse("\") Then
                Exit For
            End If
            bmname = bmname + c
        Next

        bmname = mypicturefolder & "\" & StrReverse(bmname)

        Dim width As Integer = fileinfo.Width   'image width.
        Dim height As Integer = fileinfo.Height   'image height

        Dim thumb As New Bitmap(width, height)
        Dim g As Graphics = Graphics.FromImage(thumb)

        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        g.DrawImage(bm, New Rectangle(0, 0, width, height), _
        	New Rectangle(0, 0, bm.Width, bm.Height), GraphicsUnit.Pixel)
        g.Dispose()

        Try
            Select Case ComboBox1.SelectedIndex
                Case 0
                    Exit Sub
                Case 1
                    thumb.Save(bmname & ".bmp", Imaging.ImageFormat.Bmp)
                Case 2
                    thumb.Save(bmname & ".jpg", Imaging.ImageFormat.Jpeg)
                Case 3
                    thumb.Save(bmname & ".gif", Imaging.ImageFormat.Gif)
                Case 4
                    thumb.Save(bmname & ".ico", Imaging.ImageFormat.Icon)
                Case 5
                    thumb.Save(bmname & ".png", Imaging.ImageFormat.Png)
                Case 6
                    thumb.Save(bmname & ".tiff", Imaging.ImageFormat.Tiff)
                Case 7
                    thumb.Save(bmname & ".wmf", Imaging.ImageFormat.Wmf)
            End Select
            CheckedListBox1.Items.Add(bmname & "." & _
            	Strings.LCase(ComboBox1.Items(ComboBox1.SelectedIndex)), True)
        Catch ex As Exception
            CheckedListBox1.Items.Add(bmname & "." & _
            	ComboBox1.Items(ComboBox1.SelectedIndex), False)
        End Try

        bm.Dispose()
        thumb.Dispose()

    End Sub

...

The below code accesses the path of 'My Picture' directory from register to save the converted image:

       Dim rk1 As Microsoft.Win32.RegistryKey
       Dim getval As String
       mypicturefolder = ""
       rk1 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey_
	("Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", False)
       getval = rk1.GetValue("My Pictures", "NULL")
       rk1.Close()
...

When images are dropped to the left box from the explorer, it checks for the following:

  1. If it is an image file or not. If not, then don't add to the list.
  2. If the image file is already added to the list, then don't add it again.

Select the format to be converted, and then click on the image.

The following image shows how it works.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here