How to Create a Image Viewer in Visual Basic
Submitted by Yorkiebar on Friday, October 11, 2013 - 06:20.
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).
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.
Project Complete!
That's it! Below is the full source code and download to the project files.
- Dim defaultPath As String = "G:\Libaries\Pictures"
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- For Each foundFile As String In My.Computer.FileSystem.GetFiles(defaultPath)
- 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))
- Next
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- If (Not ListBox1.SelectedIndex < 0) Then
- Try
- Dim img As Image = Image.FromFile(defaultPath & "\" & ListBox1.SelectedItem.ToString())
- PictureBox1.BackgroundImage = img
- Catch ex As Exception
- End Try
- End If
- End Sub
- Public Class Form1
- Dim defaultPath As String = "G:\Libaries\Pictures"
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- For Each foundFile As String In My.Computer.FileSystem.GetFiles(defaultPath)
- 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))
- Next
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- If (Not ListBox1.SelectedIndex < 0) Then
- Try
- Dim img As Image = Image.FromFile(defaultPath & "\" & ListBox1.SelectedItem.ToString())
- PictureBox1.BackgroundImage = img
- Catch ex As Exception
- End Try
- End If
- End Sub
- 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
- 785 views