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
Dim height As Integer = fileinfo.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:
- If it is an image file or not. If not, then don't add to the list.
- 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.