Picture Viewer

 

This is a program that enables the user  to open and choose image files from the folders in his or her  computer and view them in a picture box. There are two ways to implement the program. The first is to build the program from the ground up and the second makes use of the common dialog box.

The first way is a little more complicated but you can learn a great deal of programming techniques. To create this program, you need to insert a a drive list box(DriveListBox) , a directory list box (DirListBox), a file list box(FileListBox) and a combo box . The drive list box is for the user to select a drive, the directory list box is for the user to choose a folder and the file list box is display the files in the selected folder. Besides that, the combo box allows the user to select all graphics files or all files. You also need to insert a picture box to display the image.

 

The Interface

The code

Private Sub Combo1_Change()
'To list all graphics files or all files
If ListIndex = 0 Then
File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")
Else
Fiel1.Pattern = ("*.*")
End If
End Sub

Private Sub Dir1_Change()
'To choose drive
File1.Path = Dir1.Path
File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")
End Sub

Private Sub Drive1_Change()
Dir1.Path = Drive1.Drive
End Sub

Private Sub File1_Click()
'To select a file
If Combo1.ListIndex = 0 Then
File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")
Else
File1.Pattern = ("*.*")
End If

If Right(File1.Path, 1) <> "\" Then
filenam = File1.Path + "\" + File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
End Sub
 

Private Sub show_Click()
'To show the selected graphics file
If Right(File1.Path, 1) <> "\" Then
filenam = File1.Path + "\" + File1.FileName
Else
filenam = File1.Path + File1.FileName
End If
Picture1.Picture = LoadPicture(filenam)
End Sub

 

 

 

 



 

 



 

 

The second way is much easier to program, you only need to insert an image control, a common dialog box and an icon that resembles on opened file. You need to set the stretchable property of the image control to true.  The procedure to open the common dialog box to browse the image files as well as to load the selected picture into the image control is

CommonDialog1.Filter = "Bitmaps(*.BMP)|*.BMP|Metafiles(*.WMF)|*.WMF|Jpeg Files(*.jpg)|*.jpg|GIF Files(*.gif)|*.gif|Icon Files(*.ico)|*.ico|All Files(*.*)|*.*"

CommonDialog1.ShowOpen

Picture1.Picture = LoadPicture(CommonDialog1.FileName)

The filter property of the common dialog box uses the format as shown below

Bitmaps(*.BMP)|*.BMP

to specify the file type, and uses the pipe line | to separate different file types.

Visual Basic supports most of the picture formats namely bmp, wmf, jpg, gif, ico(icon) and cur(cursor) files.

The command  CommonDialog1.ShowOpen is to open the common dialog box and the command

 Picture1.Picture = LoadPicture (CommonDialog1.FileName)

is to load the selected picture file into the picture box.

 The whole program is shown below and the output is shown in the figure below:

 Private Sub Image1_Click()

CommonDialog1.Filter = "Bitmaps(*.BMP)|*.BMP|Metafiles(*.WMF)|*.WMF|Jpeg Files(*.jpg)|*.jpg|GIF Files(*.gif)|*.gif|Icon Files(*.ico)|*.ico|All Files(*.*)|*.*"

CommonDialog1.ShowOpen

image2.Picture = LoadPicture (CommonDialog1.FileName)

End Sub

When the user clicks the opened file icon, the following dialog will appear. The user then can select the file he or she wish to view.

 

The Interface

 [Back to VB Sample Programs]

Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: admin@vbtutor.net

[Privacy Policy]