Sunday, 15 July 2007

.Net Photo Gallery Control (PhotoHandler)

I had a quick Google for a .Net control or library for creating an online photo album easily - and therefore save me some time in writing my own. I then found a reference on ScottGu's blog about a PhotoHandler, which is a little project of Bertrand Le Roy. It is basically one C# code file that can be used as a control on a page or you can just browse to the file itself and it will display the folders and images in the current directory. The best thing about it is the simple setup and you can just ftp some image folders onto your server and the control will just display it all for you - no need to update any aspx/content pages. Download the source from the projects PhotoHandler CodePlex pages. You'll notice a samples directory - these pages use the handler as a control which is how I like to use it. Useful points on PhotoHandler usage: Get the required MetaDataExtractor library and pop it in your web apps bin directory. Also, make sure you get version V2.2.2d - otherwise you'll get a The type or namespace name 'Directory' does not exist in the namespace 'com.drew.metadata' (are you missing an assembly reference?) error. If you are using the control in a sub directory of your site then yo need to set the HandlerUrl property on the control or you'll get no images or thumbnails displayed. If you want to hide some folders from the album viewer you can make a mod to the control. On line 1648 where the code checks for a few common .net directories:
if (dir.StartsWith("_vti_") || dir.StartsWith("app_") ||
    (dir == "bin") || (dir == "aspnet_client")) 
{
   continue;
}
Change this to :
if (IsHiddenFolder(dir)) 
{
    continue;
}
Add the two helper methods:
// Checks a string[] to see if the folder should be omitted
private bool IsHiddenFolder(string directoryName)
{
    if (directoryName.StartsWith("_vti_") || directoryName.StartsWith("app_") )
    {
        return true;
    }

    if (!isHiddenListLoaded)
    {
        LoadHiddenFolderList();
    }

    for (int i = 0; i < hiddenList.Length; i++)
    {
        if( hiddenList[i].ToLower() == directoryName)
        {
            return true;   
        }
    }

    return false;
}

// Loads a string[] from a text file
private void LoadHiddenFolderList()
{
    using (StreamReader reader = new StreamReader(Server.MapPath("HiddenList.txt")))
    {
        ArrayList lines = new ArrayList();

        while (reader.Peek() != -1)
        {
            lines.Add(reader.ReadLine().Trim());
        }

        hiddenList = lines.ToArray(typeof(string)) as string[];
    }

    isHiddenListLoaded = true;
}
Finally, put the text file with directories (one per line) to omit as HiddenList.txt in the same location as the control and add two static variables to the class required by the helper methods:
private static bool isHiddenListLoaded;
private static string[] hiddenList;
Goodluck :)

2 comments:

Anonymous said...

thank you very much, very helpful

Anonymous said...

Thanks for the input. When I searched PhotoHandler in google your blog appeared. It helped me to address the Metadataextractor compiler issue and hidden dir management.

Thanks and keep it up