VB Tutor VB.NET 2022 Tutorial VB2019 Tutorial VB6 Tutorial VB Sample Code About Us
Visual Basic Sample Code

Picture Viewer

Create an image viewer application in Visual Basic 6 and VB.NET


Introduction

This program enables users to search for image files on their computer and view them in a picture box. We'll explore two implementation methods for VB6 and their equivalent in VB.NET.

File System Controls

Use DriveListBox, DirListBox and FileListBox to create a file browser interface

Common Dialog

Simpler method using CommonDialog control to open image files

VB.NET Version

Modern implementation using OpenFileDialog and PictureBox

Tip: The Common Dialog method is simpler to implement while the File System Controls method gives you more control over the browsing interface.

Visual Basic 6 Implementation

Using File System Controls

This method uses DriveListBox, DirListBox, and FileListBox controls to create a file browser interface. It requires more setup but provides a fully integrated browsing experience.

Interface Design

Picture Viewer Interface

VB6 Code

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

Private Sub Dir1_Change()
    'Update file list when directory changes
    File1.Path = Dir1.Path
    File1.Pattern = ("*.bmp;*.wmf;*.jpg;*.gif")
End Sub

Private Sub Drive1_Change()
    'Update directory when drive changes
    Dir1.Path = Drive1.Drive
End Sub

Private Sub File1_Click()
    'Handle file selection
    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()
    'Display selected image
    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

VB.NET Implementation

In VB.NET, we use the OpenFileDialog control to select images and the PictureBox control to display them.

VB.NET Code

Private Sub btnBrowse_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
    'Configure OpenFileDialog
    With OpenFileDialog1
        .Title = "Select Image File"
        .Filter = "Image Files|*.bmp;*.jpg;*.jpeg;*.gif;*.png;" & _
                  "*.tif;*.tiff;*.ico|All Files|*.*"
        .Multiselect = False
    End With
    
    'Show dialog and handle selection
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
        Try
            'Load the selected image
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
            
            'Update status
            lblStatus.Text = Path.GetFileName(OpenFileDialog1.FileName)
            lblSize.Text = $"Size: {New FileInfo(OpenFileDialog1.FileName).Length / 1024:n0} KB"
        Catch ex As Exception
            MessageBox.Show("Error loading image: " & ex.Message, 
                            "Image Error", 
                            MessageBoxButtons.OK, 
                            MessageBoxIcon.Error)
        End Try
    End If
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'Set initial PictureBox properties
    PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
    PictureBox1.BorderStyle = BorderStyle.Fixed3D
End Sub

Key Differences

  • Dialog Control: OpenFileDialog replaces CommonDialog
  • Image Loading: Image.FromFile() instead of LoadPicture
  • Error Handling: Structured Try-Catch blocks
  • File Info: Use System.IO for file information
  • Modern Syntax: String interpolation ($"") for formatting

Note: VB.NET provides better image handling capabilities with support for more formats and additional features like zooming and panning.

Interactive Demo

Try the Picture Viewer functionality right in your browser. Select an image from the file browser to view it.

File Browser

  • Mountain Landscape.jpg
  • Forest Path.jpg
  • Ocean Beach.jpg
  • Lion Portrait.jpg
  • Flying Eagle.jpg
  • Dolphins.jpg
  • Colorful Abstract.jpg
  • Geometric Pattern.jpg

Image Preview

Preview
Select an image to preview
File: No file selected
Type: -
Size: -

Additional Resources

Recommended Books

VB Programming Book

Visual Basic Programming with Code Examples

Comprehensive guide covering VB6 and VB.NET with practical examples.

View on Amazon