How to Create a Image Viewer in Visual Basic

Language
Introduction: Welcome to a tutorial on how to create a Image Viewer in Visual Basic. Steps of Creation: Step 1: First we want to create a form with a button to laod the given image, a listbox to contain each loadable image from the given directory and a picturebox with the background image format set to zoom to display the given image correctly. Step 2: Next we want to create a defaultPath to load images from as well as load the image files on form load (open program).
  1. Dim defaultPath As String = "G:\Libaries\Pictures"
  2. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  3.     For Each foundFile As String In My.Computer.FileSystem.GetFiles(defaultPath)
  4.         If (foundFile.ToLower().EndsWith(".png") Or foundFile.ToLower().EndsWith(".ico") Or foundFile.ToLower().EndsWith(".jpg") Or foundFile.ToLower().EndsWith(".jpeg") Or foundFile.ToLower().EndsWith(".gif")) Then ListBox1.Items.Add(foundFile.Split("\")(foundFile.Split("\").Count() - 1))
  5.     Next
  6. End Sub
Step 3: Now we want to simply load the selected image file. We want to create an image variable with the default path and selected image file from the listbox (file name and extension) and set the pictureboxes background image to the created variable of image.
  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2.     If (Not ListBox1.SelectedIndex < 0) Then
  3.         Try
  4.             Dim img As Image = Image.FromFile(defaultPath & "\" & ListBox1.SelectedItem.ToString())
  5.             PictureBox1.BackgroundImage = img
  6.         Catch ex As Exception
  7.         End Try
  8.     End If
  9. End Sub
Project Complete! That's it! Below is the full source code and download to the project files.
  1. Public Class Form1
  2.     Dim defaultPath As String = "G:\Libaries\Pictures"
  3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  4.         For Each foundFile As String In My.Computer.FileSystem.GetFiles(defaultPath)
  5.             If (foundFile.ToLower().EndsWith(".png") Or foundFile.ToLower().EndsWith(".ico") Or foundFile.ToLower().EndsWith(".jpg") Or foundFile.ToLower().EndsWith(".jpeg") Or foundFile.ToLower().EndsWith(".gif")) Then ListBox1.Items.Add(foundFile.Split("\")(foundFile.Split("\").Count() - 1))
  6.         Next
  7.     End Sub
  8.  
  9.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  10.         If (Not ListBox1.SelectedIndex < 0) Then
  11.             Try
  12.                 Dim img As Image = Image.FromFile(defaultPath & "\" & ListBox1.SelectedItem.ToString())
  13.                 PictureBox1.BackgroundImage = img
  14.             Catch ex As Exception
  15.             End Try
  16.         End If
  17.     End Sub
  18. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment