Automatically generating thumbnail images in SharePoint

When you upload images to a SharePoint picture library three images are actually stored. Firstly there’s the actual image that you uploaded then there are two others with specific sizes. There’s a thumbnail image that’s scaled to a particular width and is used in the default ‘All Pictures’ view then there’s a web image that’s resized to a specific width and height. The thumbnails are saved to a hidden folder in the picture library named _t and the web images are saves to a hidden folder named _w.

Let’s say you have a few web parts that make use of a SharePoint Picture Library to store images but you need to display the images in specific sizes that don’t match the default values for the SPPictureLIbrary. What are your options?

You have a few:

Firstly, you could create a whole load of CAML to define a new type of Picture Library inheriting from Type 109 (the built-in picture library). In the CAML you could set the ThumbnailSize, WebImageWidth and WebImageHeight attributes on the List element.

Secondly, as mentioned here you could use a tool to change the properties of the picture library.

Finally, and the approach that I decided to take -  you could create a simple feature receiver that accepted these values as properties then applied them to a specific instance of a picture library.

Here’s the CAML for the feature.xml file:

<?xml version="1.0" encoding="utf-8"?>
<Feature  Id="84d5108a-f1e6-442b-a647-d5a866204039"
          Title="My Custom Images"
          Description="Creates a library for storing images"
          Version="12.0.0.0"
          Hidden="TRUE"
          Scope="Web"
          DefaultResourceFile="core"
          xmlns="http://schemas.microsoft.com/sharepoint/"
          ReceiverAssembly="MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=183802e36edc8515"
          ReceiverClass="MyAssembly.Receivers.ImageSizes">
  <Properties>
    <Property Key="LibraryName" Value="Custom Images"/>
    <Property Key="ThumbnailSize" Value="50"/>
    <Property Key="WebImageWidth" Value="100"/>
    <Property Key="WebImageHeight" Value="100"/>
  </Properties>
  <ElementManifests>
    <ElementManifest Location="instance.xml"/>
  </ElementManifests>
</Feature>

Here’s the source for a simple feature receiver to make the necessary changes to the SPPictureLIbrary:

namespace MyAssembly.Receivers
{
    public class ImageSizes : SPFeatureReceiver
    {

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                SPWeb currentWeb = null;

                if (site != null)
                {
                    currentWeb = site.RootWeb;
                }
                else
                {
                    currentWeb = properties.Feature.Parent as SPWeb;
                }

                using (currentWeb)
                {
                    string libraryName = properties.Definition.Properties["LibraryName"].Value;
                    int thumbnailSize = int.Parse(properties.Definition.Properties["ThumbnailSize"].Value);
                    int webImageWidth = int.Parse(properties.Definition.Properties["WebImageWidth"].Value);
                    int webImageHeight = int.Parse(properties.Definition.Properties["WebImageHeight"].Value);

                    SPPictureLibrary lib = currentWeb.Lists[libraryName] as SPPictureLibrary;
                    lib.ThumbnailSize = thumbnailSize;
                    lib.WebImageHeight = webImageHeight;
                    lib.WebImageWidth = webImageWidth;
                    lib.Update();
                }
            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties) { }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { }

    }

This entry was posted in HowTo, SharePoint. Bookmark the permalink.
  • http://www.lowest-rate-loans.com/ SwansonCarmela

    Following my own exploration, thousands of people in the world receive the business loans from various creditors. Thence, there’s good possibilities to receive a collateral loan in any country.

  • http://www.facebook.com/people/Alexander-Gorlach/100001244265440 Alexander Gorlach

    For SharePoint 2010 and 2013 here is ready to use product: HarePoint Thumbnails for SharePoint

    http://www.harepoint.com/Products/HarePointThumbnails/Default.aspx

    It create tumbnails for Office documents, PDFs, images etc.

    WBR, Alexander