Sharepoint Thumbnails and AssetUploader.aspx

We recently had a consultant build a web part for us that displayed thumbnails from images in a document library. In digging around in the code they gave us I discovered that they used a page built into Sharepoint called AssetUploader.aspx (in 12\TEMPLATE\LAYOUTS\AssetUploader.aspx, available off any web at _layouts\AssetUploader.aspx). This page is a rather handy. It’s specifically designed to take a URL of an available image, resize it, and spit out a thumbnail. You can use it in any page or web part in Sharepoint by creating an IMG tag like this:

1
<img src=”http://www.blogger.com/_layouts/AssetUploader.aspx?Size=Medium&ImageURL=/mysitecollection/mysite/MyImagesDocumentLibrary/IMG1234.jpgalt=”My Image” />

If you use a size of ‘Small’, it will make the largest dimension of the image 70 pixels, and if you use anything other than small (Medium, Large, etc.), it will make the largest dimension of the image 140 pixels.
It worked wonderfully in our test environment, but once we brought it into production, it broke. All we saw were little generic white document icons - no thumbnails. Yikes!

I turned, of course, to google, and I found a very helpful post from Bob Moore at PointBridge, where he explained the problem and provided some code as a solution. His code was tremendously helpful, but didn’t get me all the way there, so I went to reflector myself to try and disassemble and fix it on my own.

However, interestingly, reflector couldn’t disassemble all of it. The main class I was interested in was Microsoft.SharePoint.Publishing.Internal.CodeBehind.AssetThumbnailerPage. So I opened it in reflector to find this message when I tried to disassemble the OnLoad event:

This item is obfuscated and cannot be translated.

I soon found that while Reflector couldn’t show me the C# code for that function, I could look at the IL code for it (to do this, click on the language drop down in the toolbar and change it from C# or Visual Basic to IL). I’d never seen this before, but once I found Microsoft’s IL reference, I was able to decipher and disassemble it by hand back into C#.

Here is the solution I ended up with that works well with AAM. I created a simple ASPX page containing only this:

1
2
<%@ Assembly Name="MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0123456789abcdef"%>
<%@ Page Language="C#" Inherits="MyNamespace.MyAssetThumbnailerPage" %>

And here is the code for the MyAssetThumbnailerPage class, which I included in a DLL that gets deployed via a feature to the GAC:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Drawing;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Security.Permissions;
using System.IO;
using System.Globalization;
using System.Drawing.Imaging;
using System.Web;
using System.Collections;

namespace MyNamespace
{
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public partial class MyAssetThumbnailerPage : UnsecuredLayoutsPageBase
{

protected override void OnLoad(EventArgs e)
{

int Size;
SPWeb web = null;
SPSite site = null;
try
{
string ImageURL = Request.QueryString["ImageURL"].ToString();
if (Request.QueryString["Size"].ToString() == "Small")
{
Size = 70;
}
else
{
Size = 140;
}

Uri uri = new Uri(SPContext.Current.Web.Url);
string url = uri.Scheme + "://" + uri.Host + ImageURL;
Uri uri2 = new Uri(HttpContext.Current.Request.Url, url);

site = SPContext.Current.Site;
site = new SPSite(SPContext.Current.Web.Url);
SPSecurity.CatchAccessDeniedException = false;
web = site.OpenWeb();
SPFile file;
file = (SPFile)web.GetFileOrFolderObject(uri2.AbsoluteUri);
SPSecurity.CatchAccessDeniedException = true;

if (file == null)
{
Server.Transfer("/_layouts/IMAGES/ICGEN.GIF");
}

if (!IsImageUrl(file.Url))
{
Server.Transfer("/_layouts/IMAGES/" + file.IconUrl);
}

byte[] fileBytes = MyAssetThumbnailer.CreateThumbnail
(file.OpenBinary(),Size);

Response.OutputStream.Write(fileBytes, 0, fileBytes.Length);

base.OnLoad(e);
}
catch (Exception ex)
{
base.OnLoad(e);
}
finally
{
web.Close();
web.Dispose();
site.Close();
site.Dispose();
}
}

private static bool IsImageUrl(string fullUrlToTest)
{

if (!string.IsNullOrEmpty(fullUrlToTest))
{
string str = fullUrlToTest;
int index = fullUrlToTest.IndexOf
("?", StringComparison.Ordinal);
if (-1 == index)
{
index = fullUrlToTest.IndexOf
("#", StringComparison.Ordinal);
}
if (-1 != index)
{
str = fullUrlToTest.Substring(0, index);
}
string str2 = string.Empty;
if (!string.IsNullOrEmpty(str))
{
index = str.LastIndexOf(".", StringComparison.Ordinal);
if (-1 != index)
{
str2 = str.Substring(index);
}
}
//ULS.SendTraceTag(ULSTagID.tag_6osa,
//ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Medium,
//"Verifying that the extension [%s] of URL ['%s' - full: " +
//"'%s'] is a recognized image type.",
//new object[] { str2, str, fullUrlToTest });
if (string.IsNullOrEmpty(str2))
{
return false;
}
ArrayList list = new ArrayList(new string[] { ".bmp", ".gif",
".jpeg", ".jpg", ".jpe", ".png" });
return list.Contains(str2.ToLowerInvariant());
}
return false;
}



}

internal class MyAssetThumbnailer
{
private MyAssetThumbnailer()
{


}

public static byte[] CreateThumbnail(byte[] inputBuffer,
int thumbMaxDimension)
{
MemoryStream stream = new MemoryStream(inputBuffer);
Bitmap bitmap = new Bitmap(stream);
Size imageSize = bitmap.Size;
Size size2 =getThumbnailDimensions(imageSize, thumbMaxDimension);
if (size2 == imageSize)
{
object[] objArray = new object[] {
thumbMaxDimension.ToString(CultureInfo.InvariantCulture),
imageSize.Width.ToString(CultureInfo.InvariantCulture),
imageSize.Height.ToString(CultureInfo.InvariantCulture),
inputBuffer.Length.ToString(CultureInfo.InvariantCulture)
};
//ULS.SendTraceTag(ULSTagID.tag_6osl,
//ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Medium,
//"No thumbnail made - thumbnail size [%s] is greater " +
//"than image dimensions[%sx%s] bytes: %s", objArray);
return inputBuffer;
}
object[] data = new object[] {
size2.Width.ToString(CultureInfo.InvariantCulture),
size2.Height.ToString(CultureInfo.InvariantCulture),
imageSize.Width.ToString(CultureInfo.InvariantCulture),
imageSize.Height.ToString(CultureInfo.InvariantCulture),
inputBuffer.Length.ToString(CultureInfo.InvariantCulture)
};
//ULS.SendTraceTag(ULSTagID.tag_6osm,
//ULSCat.msoulscat_CMS_Publishing, ULSTraceLevel.Medium,
//"Generating thumbnail with dimensions[%sx%s] for image with " +
//"dimensions[%sx%s] bytes: %s", data);
return imageObjectToBytes(
bitmap.GetThumbnailImage(
size2.Width,
size2.Height,
new System.Drawing.Image.GetThumbnailImageAbort
(MyAssetThumbnailer.thumbnailCallback),
IntPtr.Zero
)
);
}

private static Size getThumbnailDimensions
(Size imageSize, int thumbMaxDimension)
{


if ((imageSize.Height > thumbMaxDimension) ||
(imageSize.Width > thumbMaxDimension))
{
SizeF ef = new SizeF((float)thumbMaxDimension,
(float)thumbMaxDimension);
if (imageSize.Height <= imageSize.Width)
{
ef.Height = (imageSize.Height * thumbMaxDimension) /
imageSize.Width;
}
else
{
ef.Width = (imageSize.Width * thumbMaxDimension) /
imageSize.Height;
}
if (ef.Width <>
{
ef.Width = 1f;
}
if (ef.Height <>
{
ef.Height = 1f;
}
if (ef.Width > thumbMaxDimension)
{
ef.Width = thumbMaxDimension;
}
if (ef.Height > thumbMaxDimension)
{
ef.Height = thumbMaxDimension;
}
return Size.Ceiling(ef);
}
return imageSize;
}

private static byte[] imageObjectToBytes
(System.Drawing.Image imageObject)
{
MemoryStream stream = new MemoryStream();
imageObject.Save(stream, ImageFormat.Jpeg);
return stream.ToArray();
}

private static bool thumbnailCallback()
{

return false;
}
}
}