Tuesday 25 December 2012

Gets thumbnail, full, large size url images from a sharepoint picture library


///Provides the url to a picture provided the picture library list item.
private 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();
}

 // Enum
public enum ImageSize
{
 Thumbnail,
 Full,
 Large
}

1 comment:

  1. JFYI - a solution for thumbnails preview in SharePoint 2010/2013: http://www.harepoint.com/Products/HarePointThumbnails/Default.aspx
    Note it allows to generate preview in SharePoint libraries both for images and files (.doc, .xls, .pdf, etc).

    ReplyDelete