Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  A Simple Photo Browser  »  Creating Thumbnails at Run-Time
 »  Home  »  Windows Development  »  A Simple Photo Browser  »  Creating Thumbnails at Run-Time
 »  Home  »  Windows Development  »  Win Forms  »  A Simple Photo Browser  »  Creating Thumbnails at Run-Time
A Simple Photo Browser
by Chris Mills | Published  04/14/2006 | .NET Newbie Windows Development Win Forms | Rating:
Creating Thumbnails at Run-Time

We'll now add a function to generate a new thumbnail, set its size, add it to a specified Scrollable Control and set up an event handler for its "ThumbnailDoubleClick".  The function is shown below:

    Public Function AddThumbnail(ByRef cntlThumbnailContainer As ScrollableControl, ByVal strFileName As String) As Thumbnail

        Dim tnNewThumbnail As Thumbnail

        ' Create a new thumbnail
        tnNewThumbnail = New Thumbnail(strFileName)
        ' Assign its height, width and make sure it is visible
        With tnNewThumbnail
            If .Width <> iTNWidth Then .Width = iTNWidth
            If .Height <> iTNHeight Then .Height = iTNHeight
            .Visible = True
        End With
        ' Add the thumbnail to the specified control
        ' Set up the event handler
        AddHandler tnNewThumbnail.ThumbnailDoubleClick, AddressOf OnThumbnailDoubleClick

        ' Return the thumbnail we just created
        Return tnNewThumbnail

    End Function

The next function used for the displaying of our thumbnails will be used to clear all thumbnails on the specified scrollable control.  Add the following function to your code:

    Public Sub ClearThumbnails(ByRef cntlThumbnailContainer As ScrollableControl)

        Dim i As Integer = 0
        Dim c As Control

        While i < cntlThumbnailContainer.Controls.Count
            c = cntlThumbnailContainer.Controls(i)
            If c.GetType Is GetType(Thumbnail) Then
                cntlThumbnailContainer.Controls.Remove(c) ' Remove the current thumbnail
                c.Dispose() ' Dispose of it
                i += 1
            End If
        End While

    End Sub

The loop used may seem complicated but is necessary as we are removing controls from the specified controls collection the more conventional For Each loop structure would not work as the number of controls in the collection could change after each loop iteration.

Generating Thumbnails for all Images in a Folder

Now we have all the functions to generate and display individual thumbnails we'll add a function to display thumbnails for all JPEGs in a folder, specified by the user using a FolderBrowserDialog, to the specified scrollable control.  Add this function to your forms code:

    Private Sub GenerateThumbnailsForFolder(ByRef cntlThumbnailContainer As ScrollableControl)

        Dim dlg As FolderBrowserDialog
        Dim iThumbnailCount As Integer = 0
        Dim tnNewThumbnail As Thumbnail

        ' Create a new instance of the folder browser dialog
        dlg = New FolderBrowserDialog

        ' Show the dialog and check that the user pressed OK to close it - not cancel
        If dlg.ShowDialog = DialogResult.OK Then

            Me.Cursor = Cursors.WaitCursor ' Set the Forms cursor to a hour glass

            ' Clear all thumbnails currently on the control

            ' Get all the JPEG files in the specified folder and loop through them
            For Each objFile As FileInfo In New DirectoryInfo(dlg.SelectedPath).GetFiles("*.jpg")
                ' Create a new thumbnail to display the current file
                tnNewThumbnail = AddThumbnail(cntlThumbnailContainer, objFile.FullName)
                ' Arrange the thumbnail
                SetThumbnailPosition(tnNewThumbnail, cntlThumbnailContainer, iThumbnailCount)
                iThumbnailCount += 1  ' Increment the count of the thumbnails on the control

            ' Set the title of the application to reflect the folder name being browsed
            Me.Text = Application.ProductName & " - " & dlg.SelectedPath
            Me.Cursor = Cursors.Arrow ' Return the cursor to an arrow
        End If
        ' Dispose of the dialog

    End Sub

This function uses the DirectoryInfo class to get all the JPEG images in the selected folder.  It then uses the functions we created to display and arrange the thumbnails on the specified control.  While the thumbnails are being generated we set the forms cursor to be an hour glass to indicate to the user that the application is busy, the cursor is restored to an arrow when the operation is complete.

We are now ready to add event handlers to the "Browse…" menu on our form.  It would have been possible to add the code in the "GenerateThumbnailsForFolder" function to this event handler; however this is not good practice.  In modern Windows application there is often more than one way of performing the same task, for example a main menu item, context menu item and toolbar button may all perform the same, or very similar tasks - to prevent adding the same large block of code several times in an application we create a function to do all the hard work and simply call this function from the required event handler.  Add the following to your code:

    Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOpen.Click
        GenerateThumbnailsForFolder(DirectCast(pnlThumbnails, ScrollableControl))
    End Sub

We simply call the function "GenerateThumbnailsForFolder" passing in the control the thumbnails are to be displayed on.  We have to cast the Panel to a ScrollableControl, which may seem unnecessary, but by passing in a reference to a ScrollableControl we can pass in a reference any control that is inherited from the ScrollableControl base class (either a Panel or Form - as before, this may make this code a little more useful in future applications.

Finally we'll add an event handler to allow the user to close the application from the menu we created earlier.  The code is shown below:

    Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
    End Sub

Whilst the application is far from finished, it is how ready for to view its first images!

Sponsored Links