Image Converter Using Visual Basic.Net

This tutorial will teach you on how to create a simple Image Converter using Visual Basic. This program is able to change the image format like converting JPEG format to PNG format. To build this program open Visual Basic and create a new project and name it as Image Converter. Then add two buttons, one OpenFileDialog, one SaveFileDialog, one Combobox, one Label and one Picturebox.

Object	 			Property			Settings

Button1				Name				btnbrowseimg
				Text				Browse Image
Button2				Name				btnConvert
				Text				Convert Now
OpenFileDialog1			Name				OFDialog
SaveFileDialog1							saveDialog
Combobox1			Name				cbformat
				Items				PNG
								BMP
								GIF
								JPEG
Label1				Text				Image Converter
Picturebox1			Name				PicBox
Form1				Name				imgfrm
				Text				Image Converter
				StartupPosition			CenterScreen
				Show Icon			False
The User interface looks like as shown below. After setting the user interface, add the following code to “Browse Image” button. To do this double click this button and add the code below.
  1. dim val as boolean
  2.  
  3. Private Sub btnbrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbrowse.Click
  4.  
  5. Try
  6. ' when the Browse Image button is click it will open the OpenfileDialog
  7. 'this line of will check if the dialogresult selected is cancel then
  8. If OFDialog.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
  9. 'the the selected image speciied by the user will be put into out picturebox
  10. picbox.Image = Image.FromFile(OFDialog.FileName)
  11. 'then the image sizemode is set to strectImage
  12. picbox.SizeMode = PictureBoxSizeMode.StretchImage
  13. 'then we assign val in to true because val variable above is declare as boolean
  14. val = True
  15. End If
  16. Catch ex As Exception
  17. MsgBox(ex.Message)
  18. End Try
  19. End Sub
And then we are now going to add the code for “Convert” Button. The code is:
  1. Private Sub btnconvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconvert.Click
  2. Try
  3. 'it check if the variable val is set to val
  4. If val = True Then
  5. 'check if what format is selected by user in the combobox
  6. 'the after this the program will convert it to the desired format by the user.
  7. If cbformats.SelectedItem = "PNG" Then
  8. saveDialog.Filter = "PNG|*.png"
  9. If saveDialog.ShowDialog <> Windows.Forms.DialogResultDialogResult.Cancel Then picbox.Image.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Png)
  10.  
  11. ElseIf cbformats.SelectedItem = "JPEG" Then
  12. saveDialog.Filter = "JPEG|*.jpg"
  13. If saveDialog.ShowDialog <> Windows.Forms.DialogResult.Cancel Then picbox.Image.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg)
  14. ElseIf cbformats.SelectedItem = "BMP" Then
  15. saveDialog.Filter = "BMP|*.bmp"
  16. If saveDialog.ShowDialog <> Windows.Forms.DialogResult.Cancel Then picbox.Image.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
  17. ElseIf cbformats.SelectedItem = "GIF" Then
  18. saveDialog.Filter = "GIF|*.gif"
  19. If saveDialog.ShowDialog <> Windows.Forms.DialogResult.Cancel Then picbox.Image.Save(saveDialog.FileName, System.Drawing.Imaging.ImageFormat.Gif)
  20. End If
  21. End If
  22. Catch ex As Exception
  23. MsgBox(ex.Message)
  24. End Try
  25. End Sub
Now lets try to run our program. When the program is running you can click the “Browse Image” button then select the specific image then as expected the image selected will display on the picturebox then click the “Convert Now” button and then the savefiledialog will show and write the image you desire and finally click “Save” button. After this step you can now check if the image is successfully saved to a new format. That’s it for now folks. See you in my next tutorial thanks for reading.

Add new comment