NOTE : This post is a part of a series. Read first part if you’ve missed it.
So, after we have uploaded the images, now it’s time to create a user control that will display them. In case you are unfamiliar with using User Controls in Sitefinity consult Developer Manual on that subject. You can also check this post out to see how to work with a “live user control”.
To start with we’ll add four properties to our User Control.
- GalleryTitle - string value representing the title of the gallery
- RepeatColumns - integer value representing how many columns should gallery have
- MaxThumbWidth - integer value representing maximum width of a thumbnail in pixels
- GalleryFolder - string value representing the relative path to folder which contains the image files for the gallery (this is how we determine which gallery will be represented)
Next thing we are going to do is to declare a new class MyImage, which will represent a particular image inside of the gallery. This class contains only 5 properties and no methods.
- ImageUrl - the full url to the image
- ActualWidth - the actual width of an image in pixels
- ActualHeight - the actual height of an image in pixels
- ThumbWidth - the width of a thumbnail image in pixels
- ThumbHeight - the height of a thumbnail image in pixels
Now what’s left is to find all the image files inside of the Gallery Folder, create objects of type MyImage for each of them and bind the DataList control to the Array List containing these MyImage objects. All this will do in Page_Load method. Below is the link for the full source of the User Control code file.
OPEN THE CODE IN NEW WINDOW
Here you can take a look at the User Control source :
OPEN THE CODE IN NEW WINDOW
So that’s all there is to it. One more thing should be noted though. The thumbnail resizing is done with HTML width and height attributes, which is obviously not a true resizing. This means that the actual size of file (in kb) is same as for the large image. This is not an optimal solution, but it will do for this example.
If you decide that you want to use actual thumbnails there are three different approaches that you can take :
- Manually create thumbnail images with free software such as Picasa and upload them together with the original images. You would need to modify the code a bit to look for thumbnail images as well as for the originals.
- Secon option you have is to map image files (.jpg and .gif) to Asp.NET engine and write a http handler that would dynamically resize images.
- Third option would be to dynamically create thumbnails and save them in a separate folder. This approach is actually a mix of first two approaches.
The true resizing is actually beyond the scope of this post so I won’t elaborate any more on that, but if you are persistent enough we just may make a project out of it and write a tutorial on dynamic resizing with Sitefinity
I hope you’ll find a use of this project.