I recently created a web part for a client that shows a slide show from a picture library using the Slide Show Extender from ASP.NET AJAX Control Toolkit. This requires creating a web service that returns an array of slides, each of which has a URL to a picture.
Amazingly, the most time consuming part of this was determining the URL for a thumbnail image from a picture in a picture library. There may have been a better resource, but I found the hints I needed here: http://msdn2.microsoft.com/en-us/library/ms413130.aspx.
Since this was painful enough, I thought I would post my solution so that others may benefit. Below is the source needed to get the URL for the full image, the larger size (the one you see when viewing or editing an individual picture library item), or the thumbnail size.
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
<namespace and class definition here…>
public enum ImageSize
{
Thumbnail,
Large,
Full
}
/// <summary>
/// Provides the url to a picture provided the picture library list item.
/// </summary>
/// <param name="item"></param>
/// <param name="imageSize"></param>
/// <returns></returns>
/// <remarks>
/// Ugly, but see: http://msdn2.microsoft.com/en-us/library/ms413130.aspx - there doesn't seem to be a better way.
/// </remarks>
public static string GetPictureUrl(SPListItem listItem, ImageSize imageSize)
{
StringBuilder url = new StringBuilder();
// Build the url up until the final portion
url.Append(SPEncode.UrlEncodeAsUrl(listItem.Web.Url));
url.Append('/');
url.Append(SPEncode.UrlEncodeAsUrl(listItem.ParentList.RootFolder.Url));
url.Append('/');
// Determine the final portion based on the requested image size
string filename = listItem.File.Name;
if (imageSize == ImageSize.Full)
{
url.Append(SPEncode.UrlEncodeAsUrl(filename));
}
else
{
string basefilename = Path.GetFileNameWithoutExtension(filename);
string extension = Path.GetExtension(filename);
string dir = (imageSize == ImageSize.Thumbnail) ? "_t/" : "_w/";
url.Append(dir);
url.Append(SPEncode.UrlEncodeAsUrl(basefilename));
url.Append(SPEncode.UrlEncodeAsUrl(extension).Replace('.', '_'));
url.Append(".jpg");
}
return url.ToString();
}