Easy Picture Viewer

This program was created in Microsoft Visual Basic 2010 Express to demonstrate how to use OpenFileDialog control. Create a new Windows Form Application project and add a PictureBox, three Buttons, and the OpenFileDialog controls to the form and arrange as in the picture below. Set control properties:
Control Property Value
PictureBox1 Anchor Top, Bottom, Left, Right
Size Mode Zoom
Button1 Name btnOpenFiles
Text OopenFiles
Button2 Name btnPrev
Enabled False
Text Previous
Button3 Name btnNext
Enabled False
Text Next
OpenFileDialog1 No Changes
The code for setting the file filter is OpenFileDialog1.Filter = "Pictures Files|*.jpg;*.gif;*.tif;*.png;*.bmp|All files|*.*" The filter text is in the form of text descriptor and search string separated by a pipe (|). The search string is usually asterisk (*.) followed by the file extension desired. The code above will result in the drop down list shown in the picture below. If desired, you can have each file type on a separate line in the drop down list by changing the code as follows OpenFileDialog1.Filter = "JPEG|*.jpg|GIF|*.gif|TIFF|*.tif|PNG|*.png|BMP|*.bmp|All files|*.*" The drop down list now looks like this In order to select more than one file at the time, the following code is needed OpenFileDialog1.Multiselect = True I put these two lines of code in the Form1_Load event but can be set in the design properties if desired.
  1. Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  2.      OpenFileDialog1.Filter = "Pictures Files|*.jpg;*.gif;*.tif;*.png;*.bmp|All files|*.*"
  3.      OpenFileDialog1.Multiselect = True
  4. End Sub
To call the File Dialog, start by clearing the text in the initial filename and use the ShowDialog Function to generate the File Dialog window. When the File Dialog window closes it generates a return code. Typically, the only error that you should be concerned about is if the Cancel button was selected or the window is closed without selecting a file. To catch this, use the If-Then statement below to exit the subroutine if the Cancel return code is generated
  1. OpenFileDialog1.FileName = ""
  2. If OpenFileDialog1.ShowDialog() = vbCancel Then Exit Sub
The list of filenames are recovered with the following  Filenames = OpenFileDialog1.FileNames Where Filenames is an undimensioned array Dim Filenames() As String The array is zero-based so the list of files will be indexed from 0 to N. So to find out how many files were selected, you can either use Filenames.length which will have a value of N+1 or you can us UBound(Filenames) which will have a value of N. For this program, UBound will be used. If more than one filename was selected, the Next Button is enabled otherwise it will be disabled to prevent an error. The Previous Button will be disabled to also prevent an error.
  1. If UBound(Filenames) = 0 Then btnNext.Enabled = False Else btnNext.Enabled = True
  2. btnPrev.Enabled = False
Start the file counter to zero and load the first picture into the PictureBox Image using the following
  1. FilenamesCounter = 0
  2. PictureBox1.Image = Image.FromFile(Filenames(0))
To move to the next picture, increment the file counter and enable the Previous Button. Check to see if you have reached the last file in the list; if so, disable the Next Button. Load the next picture into the PictureBox.
  1. FilenamesCounter = FilenamesCounter + 1
  2. btnPrev.Enabled = True
  3. If FilenamesCounter = UBound(Filenames) Then btnNext.Enabled = False
  4. PictureBox1.Image = Image.FromFile(Filenames(FilenamesCounter))
To return to the previous picture, decrement the file counter and enable the Next Button. If you have reached the first file, disable the Previous Button.
  1. FilenamesCounter = FilenamesCounter - 1
  2. btnNext.Enabled = True
  3. If FilenamesCounter = 0 Then btnPrev.Enabled = False
  4. PictureBox1.Image = Image.FromFile(Filenames(FilenamesCounter))
The complete code
  1. Dim Filenames() As String
  2. Dim FilenamesCounter As Integer
  3.  
  4. Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  5.     OpenFileDialog1.Filter = "Pictures Files|*.jpg;*.gif;*.tif;*.png;*.bmp|All files|*.*"
  6.     'OpenFileDialog1.Filter = "JPEG|*.jpg|GIF|*.gif|TIFF|*.tif|PNG|*.png|BMP|*.bmp|All files|*.*"
  7.     OpenFileDialog1.Multiselect = True
  8. End Sub
  9.  
  10. Private Sub btnOpenFiles_Click(sender As System.Object, e As System.EventArgs) Handles btnOpenFiles.Click
  11.     OpenFileDialog1.FileName = ""
  12.     If OpenFileDialog1.ShowDialog() = vbCancel Then Exit Sub 'Cancel
  13.     Filenames = OpenFileDialog1.FileNames
  14.     If UBound(Filenames) = 0 Then btnNext.Enabled = False Else btnNext.Enabled = True
  15.     btnPrev.Enabled = False
  16.     FilenamesCounter = 0
  17.     PictureBox1.Image = Image.FromFile(Filenames(0))
  18. End Sub
  19.  
  20. Private Sub btnPrev_Click(sender As System.Object, e As System.EventArgs) Handles btnPrev.Click
  21.     PictureBox1.Image.Dispose()
  22.     FilenamesCounter = FilenamesCounter - 1
  23.     btnNext.Enabled = True
  24.     If FilenamesCounter = 0 Then btnPrev.Enabled = False
  25.     PictureBox1.Image = Image.FromFile(Filenames(FilenamesCounter))
  26. End Sub
  27.  
  28. Private Sub btnNext_Click(sender As System.Object, e As System.EventArgs) Handles btnNext.Click
  29.     PictureBox1.Image.Dispose()
  30.     FilenamesCounter = FilenamesCounter + 1
  31.     btnPrev.Enabled = True
  32.     If FilenamesCounter = UBound(Filenames) Then btnNext.Enabled = False
  33.     PictureBox1.Image = Image.FromFile(Filenames(FilenamesCounter))
  34. End Sub

Add new comment