• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create a Image Viewer in Visual Basic

austiis

Waifu Strategist
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
56
Likes
65
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. Private

    Sub

    Button1_Click(sender As

    Object

    , e As

    EventArgs) Handles Button1.Click
  9. If

    (Not

    ListBox1.SelectedIndex < 0) Then
  10. Try
  11. Dim

    img As

    Image = Image.FromFile(defaultPath & "\"

    & ListBox1.SelectedItem.ToString())
  12. PictureBox1.BackgroundImage = img
  13. Catch ex As

    Exception
  14. End

    Try
  15. End

    If
  16. End

    Sub
  17. 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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top