How to Display Images in a Folder with VB6 Program ?

Wednesday, August 26, 2009 ·

VB6 application to display images in a directory













In this posting I will write a Sample of Simple VB6 program to display images in a directory of your computer. To display image in VB6 programming you can use LoadPicture statement. The format of statement is:
LoadPicture (Path\FileName)
For example:
LoadPicture ("Image01.jpg") -> display image named Image01.jpg in the current folder (the folder of image and the program is the same)
LoadPicture ("d:\MyPicture\Image02.bmp") -> display image named Image02.bmp that stored in folder MyPicture in drive d:

To make a VB6 program application like the above illustration you can follow below VB6 Programming tips.
In this case assumed that you have some images file (with extention .jpg or .bmp) that stored in directory D:\MyPicture.
Now you have to create a screen design like below image:
Screen Design VB6 Program Display Image













To create the screen design, please follow below steps:

  • Open your Visual Basic software
  • Select a standard exe project, let with default name (Project1 and Form1)
  • Add a label (Label1) control to your form, change its caption into "My Photo Collections", and change font size into 14
  • Add a DriveListBox (Drive1) control to your form
  • Add a Dir DirListBox (Dir1) control to your form
  • Add a FileListBox (File1) control to your form
  • Add an Image (Image1) control to your form
  • Arrange and resize the controls like the above screen design.

    Ok, program interface already done. Now the time to write code statements to functionate the controls, so the program can work.
  • Double-click Drive1 to create a Sub Drive1_Change()
  • Write below Code Statements in the Sub Drive1_Change()
      Dir1.Path = Drive1.Drive
    File1.Path = Dir1.Path

  • Double-click Dir1 to create a Sub Dir1_Change()
  • Write below Code Statement in Sub Dir1_Change()
        File1.Path = Dir1.Path

  • Double-click File1 to create a Sub File1_Click()
  • Write the code statements below in the Sub File1_Click()
        On Error GoTo ErrHandle
    Image1.Picture = LoadPicture(File1.Path & "/" & File1.FileName)
    Exit Sub

    ErrHandle:
    MsgBox "Invalid Image display"
    Resume Next

    So your Program coding will look like below listing:
    Private Sub Dir1_Change()
    File1.Path = Dir1.Path
    End Sub

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

    Private Sub File1_Click()
    On Error GoTo ErrHandle
    Image1.Picture = LoadPicture(File1.Path & "/" & File1.FileName)
    Exit Sub

    ErrHandle:
    MsgBox "Invalid Image display"
    Resume Next
    End Sub

    Now the time to test your program, follow below tips:
  • Click Start button on your VB standard toolbar
  • Select Drive D: in the DriveListBox
  • Select folder D:\MyPicture in the DirListBox
  • Select file name you want in FileListBox

    Your program should like the illustration on the top of this posting, however the image you display depend on the image you have and select.


  • 1 comments:

    Anonymous said...
    August 27, 2009 at 4:33 PM  

    Thanks for post. It’s really imformative stuff.
    I really like to read.Hope to learn a lot and have a nice experience here! my best regards guys!
    --
    rockstarbabu
    --

    SEO----SEO

    Post a Comment