ASP.NET :: CSS Image Gallery User Control
An image gallery implemented as an ASP.NET web user control.
The gallery control outputs automatic JavaScript, DHTML and uses a CSS layout for quick display and navigation through sets of image thumbnails. It also degrades gracefully for older browsers or those with JavaScript or CSS disabled. Captions and image files are read from specified directories and text files, with no need to define output or settings for each individual image. Because the web gallery is encapsulated as a user control, it can be re-used over and over again for different image or photo galleries on a site.
Easy inclusion of the web user control into a ASP.NET web form:
<%@ Register TagPrefix="uc" TagName="gallery_css" Src="/common/controls/uc_gallery_css.ascx" %>
<uc:gallery_css runat="server" id="uc_gallery_css"
maxImages="4"
pageLinkText="Thumbnails "
useLargeImages="true"
popupWidthHeight="820, 620"
cssFile="/common/css/gallery_css_photos.css"
imageDir="/images/photos/"
imageDirThumbs="thumbs"
imageDirLarge="large"
/>
A portion of the code which reads and caches the image gallery directory and caption data:
//Read Directory / Get Info & Insert Into Cache
if (Directory.Exists(_imageDirFull))
{
//Read Directory / Get Info
DirectoryInfo _dir = new DirectoryInfo(_imageDirFull);
if (_dir != null)
{
FileInfo[] _files;
_files = _dir.GetFiles();
foreach (FileInfo _file in _files)
{
string fileName = _file.Name;
if (Regex.IsMatch(_imageExtensions, _file.Extension + "[,$]", RegexOptions.IgnoreCase))
{
string foundFile = _file.Name;
hImages.Add(imageCount, foundFile);
if (File.Exists(_imageDirFull + _imageDirThumbs + @"\" + foundFile))
{
hThumbs.Add(foundFile, true);
}
if (File.Exists(_imageDirFull + (foundFile.Replace(_file.Extension, "txt"))))
{
StreamReader reader;
reader = new StreamReader(_imageDirFull + (foundFile.Replace(_file.Extension, ".txt")));
string imageCaption = reader.ReadToEnd();
if (reader != null) { reader.Close(); }
imageCaption = Regex.Replace(imageCaption, "\"", "\'");
imageCaption = Regex.Replace(imageCaption, """, "\'");
imageCaption = Regex.Replace(imageCaption, "\r\n", " ");
imageCaption = Regex.Replace(imageCaption, "\r", " ");
imageCaption = Regex.Replace(imageCaption, "\n", " ");
hCaptions.Add(foundFile, imageCaption);
}
imageCount++;
}
}//end foreach FileInfo
}//end if _dir
//Insert Into Cache
Cache.Insert(hImagesCacheName, hImages, new System.Web.Caching.CacheDependency(_imageDirFull));
Cache.Insert(hThumbsCacheName, hThumbs, new System.Web.Caching.CacheDependency(_imageDirFull));
Cache.Insert(hCaptionsCacheName, hCaptions, new System.Web.Caching.CacheDependency(_imageDirFull));
Cache.Insert(imageCountCacheName, imageCount, new System.Web.Caching.CacheDependency(_imageDirFull));
...