The OpenFileDialog control lets us select a file.
- Start a new project called PictureViewer.
- Add a button named BtnGo. Change the text to "Select a Picture"
- Drag A PictureBox control to the form.
- The control will be named PictureBox1, we will leave it as that.
- Change the SizeMode to AutoSize
- Drag a OpenFileDialog control to the form. (It will appear below the form. Leave the name as OpenFileDialog1
- Write the code as shown below
Public Class Form1
Private Sub BtnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGo.Click
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.Filter = "Bitmaps |*.bmp|JPGs|*.jpg|GIFs|*.gif|All files|*.*"
OpenFileDialog1.FilterIndex = 2 'JPG
OpenFileDialog1.ShowDialog()
If (OpenFileDialog1.FileName <> "") Then
PictureBox1.Load(OpenFileDialog1.FileName)
End If
End Sub
End Class
When you run the program, click the button to open the file dialog and display the picture.
Notice that the filter is set to include the picture types *.bmp, *.jpg and *.gif.