Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  A Simple Photo Browser  »  Rearranging Thumbnails at Run-Time
 »  Home  »  Windows Development  »  A Simple Photo Browser  »  Rearranging Thumbnails at Run-Time
 »  Home  »  Windows Development  »  Win Forms  »  A Simple Photo Browser  »  Rearranging Thumbnails at Run-Time
A Simple Photo Browser
by Chris Mills | Published  04/14/2006 | .NET Newbie Windows Development Win Forms | Rating:
Chris Mills
Chris graduated from the Robert Gordon University, Aberdeen UK in 2002 with an MEng in Electronic and Computer Engineering. Chris now works for an engineering company based in Aberdeen and develops condition monitoring and data logging solutions (both hardware and software development), he also develops tools for data analysis, visualisation and data mining.  

View all articles by Chris Mills...
Rearranging Thumbnails at Run-Time

The final change to our application in this article is modifying it to rearrange the thumbnails displayed on the panel pnlThumbnails if it is resized.  We used the Dock Properties of many of the components on the form so they would automatically be easily resized with the form they are on - but if we want the thumbnails to be rearranged when the panel that contains them is resized we will have to do it ourselves!

We'll start by adding a function to rearrange the thumbnails if the panel containing them is resized.  This function calls the "SetThumbnailPosition" function we created earlier for each thumbnail on the panel containing them.  We also get the current AutoScrollPosition for the panel when we start the operation and reset it to this position when the redraw is complete; this will prevent the thumbnails panel from being scrolled back to the top after the redraw. Add the following function to your code:

    Private Sub RearrangeThumbnails(ByRef cntlThumbnailContainer As ScrollableControl)

        Dim pOldScrollPos As Point
        Dim iAddedThumbnails As Integer = 0

        ' Get the Current Autoscroll position of the control
        With cntlThumbnailContainer.AutoScrollPosition
            pOldScrollPos = New Drawing.Point(Math.Abs(.X), Math.Abs(.Y))
        End With

        ' Set the new position of each thumbnail to fill the ScrollableControl using
        ' the SetThumbnailPosition function
        For Each tnThumbnail As Thumbnail In cntlThumbnailContainer.Controls
            SetThumbnailPosition(tnThumbnail, cntlThumbnailContainer, iAddedThumbnails)
            iAddedThumbnails += 1  ' Increment the count of added thumbnails by one
        Next

        ' Set the containers AutoScroll position to where it was before it was resized
        cntlThumbnailContainer.AutoScrollPosition = pOldScrollPos
        cntlThumbnailContainer.AutoScroll = True

    End Sub

We now add an event handler that will call this function when the panel is resized.  The function is shown below.

    Private Sub pnlThumbnails_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnlThumbnails.Resize
        RearrangeThumbnails(DirectCast(pnlThumbnails, ScrollableControl))
    End Sub

All modifications are now complete and the application is ready for use!  Whilst its functionality is somewhat limited compared with many modern photo editing applications, it does allow the user to browse and view photos with relative ease. 


This is the end of this article, but obviously there is loads of scope for improvement in the application.  You are free to use the code in this article in any of your own projects.  The photo browser could be improved dramatically by relatively simple tasks like improving the user interface by adding context menus, toolbar or statusbar, or by adding the ability to save modified images (the Image object has methods for this too) - where you take it from here is up to you, but the possibilities are almost endless!

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.58653846153846 out of 5
 104 people have rated this page
Article Score64603
Sponsored Links