Click here to Skip to main content
15,884,388 members
Articles / Desktop Programming / Windows Forms

Batch Image Format Converter

Rate me:
Please Sign up or sign in to vote.
4.62/5 (4 votes)
4 Jun 2011CPOL1 min read 29.7K   2.9K   24   2
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:

VB.NET
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:

VB.NET
       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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIcon Files Don't show Pin
S. Porat20-Mar-15 2:21
S. Porat20-Mar-15 2:21 
Question.ico and .wmf formats. Pin
Jens Madsen, Højby27-Feb-14 20:35
Jens Madsen, Højby27-Feb-14 20:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.