Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

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

There are a number of ways in which the thumbnail control we have created could be used.  For our Photo Browser Application, we want to be able view thumbnails for all the photos in a specified location; this means we will have to create -instances of our thumbnail control, add event handlers and clear the thumbnails we have created when required - all at runtime.

Adding controls to an application at run-time is pretty straightforward using Visual Basic .NET and can be a very useful technique.  All controls in the .NET Framework, including Forms, are derived from the "Control" base class.  The Control class contains a "Controls" collection, this collection contains all controls to be displayerd on the control.  If you wish to add a control to your application at runtime you create a new instance of the required control and add it to the Controls collection of the relevent control - the process is not unlike populating a TreeView or ListView at runtime except you have to specify the position of your new control manually.

Arranging Thumbnails on a Panel

Before we write any functions add the necessary Imports statements above your form code to give us easy access to the functions we will be using.  Add the following at the top of your forms code:

Imports System.IO
Imports System.Drawing

We need access to the file functions and classes in the System.IO namespace and the classes used to define location are in the "System.Drawing".

First, add set of constants to define the size and spacing of the thumbnails when displayed on the controls.  By default all control sizes and locations in the .NET framework are defined in pixels, so we will also use pixels to define the layout of our thumbnails.  Add the following at class level to your forms code.

    ' Constants to define the size and spacing of our thumbnails
    Const iTNWidth As Integer = 120
    Const iTNHeight As Integer = 120
    Const iHPadding As Integer = 5
    Const iVPadding As Integer = 5

The constants iTHWidth and iTNHeight will be used to define the size of the thumbnails, and iHPadding and iVPadding are used to define the spacing between our thumbnails.  Using constants means these values cannot be changed once the application is running and they also make it is easy change how our thumbnails are arranged on the form by simply changing the values in these constants.

The first function we'll add is to set the position of a thumbnail with in a specified scrollable control (a form or panel).  We'll pass into the function a reference to the Thumbnail control to be positioned, a reference to the control it is to be positioned on and the number of thumbnails already positioned - we'll use this to calculate the position of the specified thumbnail.  By creating our function in this way it will make it easier to reuse this function in any future applications.  Add the following function:

    Private Sub SetThumbnailPosition(ByRef tnThumbnail As Thumbnail, ByRef cntlThumbnailContainer As ScrollableControl, ByVal iThumbNailNo As Integer)

        Dim iColumns As Integer, iRow As Integer, iCol As Integer
        Dim iLeft As Integer, iTop As Integer

        ' Calculate the number of thumbnails we can fit across the control
        iColumns = (cntlThumbnailContainer.Width - 30) \ (iTNWidth + iHPadding)
        ' If the number if less then 1, set it to 1 so the thumbnails will always be displayed
        If iColumns < 1 Then iColumns = 1

        ' Calculate the position of our thumbnail in our control in terms of the
        ' row and column of grid of displayed thumbnails
        iRow = (iThumbNailNo \ iColumns) + 1
        iCol = (iThumbNailNo Mod iColumns) + 1

        ' Now calculate the position in Pixels.  We use the defined constants to calculate the
        ' position of the thumbnail and add the AutoScrollPosition of the control we are
        ' going to be arranging it on - this is necessary to ensure that the thumbnails are
        ' displayed in the correct location if the control is not currently scrolled to the top
        ' of its available area.
        iLeft = (iCol * iHPadding) + ((iCol - 1) * iTNWidth)
        iLeft += cntlThumbnailContainer.AutoScrollPosition.X()
        iTop = (iRow * iVPadding) + ((iRow - 1) * iTNWidth)
        iTop += cntlThumbnailContainer.AutoScrollPosition.Y()

        ' Set the thumbnails position
        tnThumbnail.Left = iLeft
        tnThumbnail.Top = iTop

    End Sub

We will use this function in our application to set the position of any new thumbnail control created and later to rearrange the thumbnails displayed on a panel if it is resized. 

Next add the function which we will later use to handle any Thumbnails "ThumbnailDoubleClick" event.  Add the following to your code:

    Private Sub OnThumbnailDoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim tnThumbnail As Thumbnail
        If sender.GetType Is GetType(Thumbnail) Then
            tnThumbnail = DirectCast(sender, Thumbnail)
            picPhoto.Image = Bitmap.FromFile(tnThumbnail.FileName)
        End If
    End Sub

This function will first check if the sender is a Thumbnail control, and if it is load the file specified in the Thumbnails FileName property into the PictureBox on our form.  We will use this to display the image represented by any thumbnail that is double clicked on in our PictureBox.

Comments    Submit Comment

Comment #1  (Posted by Ged on 04/14/2006)
Rating
Clear comprehensive and useful - Great stuff - Thanks!
 
Comment #2  (Posted by an unknown user on 04/21/2006)
Rating
WOW! Great! Thanks!
 
Comment #3  (Posted by an unknown user on 04/24/2006)
Rating
Very nice, would make a perfect tutorial for potential .NET programmers. Thanx
 
Comment #4  (Posted by Javier on 05/03/2006)
Rating
Excelent article, but I have a question: Does it work with vb6? Thanks
 
Comment #5  (Posted by Javier on 05/03/2006)
Rating
Hi Chris. My name is Javier. I´m writing from Argentina. I had read your article "A Simple Photo Browser" and I wish to know if that code works in vb6? I have to develop an app like that and I find your article very useful. Thank you.

PD: sorry for my bad English
 
Comment #6  (Posted by an unknown user on 10/07/2006)
Rating
Some weeks ago, I tried to make a user control on my own, but it didn't work. Now I finally got a great example of one. Thanks
 
Comment #7  (Posted by an unknown user on 11/10/2006)
Rating
Very clear and completely documented explanation!
thx
 
Comment #8  (Posted by an unknown user on 11/10/2006)
Rating
Very clear and completely documented explanation!
thx
 
Comment #9  (Posted by an unknown user on 11/21/2006)
Rating
Everything is clear and easy to understand. It helped me a lot. Thanks.
 
Comment #10  (Posted by an unknown user on 12/30/2006)
Rating
The best I've seen so far... Thanks.
 
Comment #11  (Posted by an unknown user on 01/23/2007)
Rating
Clear,Understandable and pupil not assumed to be a .Net expert.
 
Comment #12  (Posted by Graham on 01/24/2007)
Rating
This will not run, there is an error with
Dim tnThumbnail As Thumbnail
Squiggley under Thumbnail
Error Message = Type 'Thumbnail' not Defined.
 
Comment #13  (Posted by an unknown user on 01/28/2007)
Rating
This is very nice. Cleared all my querries.
 
Comment #14  (Posted by Asha on 07/13/2007)
Rating
Excellent article.Keep up the good work.Great description of code too!!
 
Comment #15  (Posted by an unknown user on 11/03/2007)
Rating
Thank you so much
 
Comment #16  (Posted by an unknown user on 11/05/2007)
Rating
Excellent article, Chris
 
Comment #17  (Posted by an unknown user on 01/02/2008)
Rating
Great article. I found it just when I need it.
 
Comment #18  (Posted by an unknown user on 02/17/2008)
Rating
thanks so much, it's really helpful
 
Comment #19  (Posted by an unknown user on 07/25/2008)
Rating
Great article!
 
Comment #20  (Posted by Anke on 10/12/2008)
Rating
but I do wonder exactly how much attention ,
 
Comment #21  (Posted by Lion on 10/12/2008)
Rating
a sort of open source world government ,
 
Comment #22  (Posted by Niro on 12/31/2008)
Rating
Nice stuff =)
Is the thumbnail panel supposed to flicker when its loading up a folder of images?
 
Comment #23  (Posted by an unknown user on 02/17/2009)
Rating
Very god!!!
 
Comment #24  (Posted by an unknown user on 04/20/2009)
Rating
Thank you, thank you. I've been looking for a way to zoom images in my own programming. This helps.
 
Comment #25  (Posted by an unknown user on 10/23/2009)
Rating
Great explaination of how code is used !
 
Comment #26  (Posted by an unknown user on 12/20/2009)
Rating
this is an excellent artical to learn for frehser
 
Sponsored Links