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
pOldScrollPos = New Drawing.Point(Math.Abs(.X), Math.Abs(.Y))
' 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
' Set the containers AutoScroll position to where it was before it was resized
cntlThumbnailContainer.AutoScrollPosition = pOldScrollPos
cntlThumbnailContainer.AutoScroll = True
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
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!