-
Notifications
You must be signed in to change notification settings - Fork 20
/
MakeThumbnail.ashx.cs
168 lines (152 loc) · 6.06 KB
/
MakeThumbnail.ashx.cs
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
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Modules.UserDefinedTable.Components;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.FileSystem;
namespace DotNetNuke.Modules.UserDefinedTable
{
/// <summary>
/// Summary description for MakeThumbnail
/// </summary>
public class MakeThumbnail : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var intMaxWidth = 0;
var intMaxHeight = 0;
Bitmap sourceImage = null;
Bitmap newImage = null;
var request = context.Request;
Stream sourceImageStream = null;
try
{
// Get max. width, if any
if (request.Params["w"] != string.Empty)
{
intMaxWidth = request.QueryString["w"].AsInt();
}
// Get max. height, if any
if (request.Params["h"] != string.Empty)
{
intMaxHeight = request.QueryString["h"].AsInt();
}
// Get source image path
var strFilepath = request.Params["image"];
//only virtual paths are valid!
var ps = PortalController.Instance.GetCurrentPortalSettings();
// Check cache for thumbnail
//add prefix to identify cache item as belonging to UDT
var cacheKey = "UDT_TN" + strFilepath + intMaxWidth + "x" + intMaxHeight;
var image = DataCache.GetCache(cacheKey);
ImageFormat iFormat;
if (image == null)
{
// Get source Image
var file = FileManager.Instance.GetFile(ps.PortalId, strFilepath);
sourceImageStream = FileManager.Instance.GetFileContent(file);
sourceImage = new Bitmap(sourceImageStream);
iFormat = ImageFormat.Jpeg;
var intSourceWidth = sourceImage.Width;
var intSourceHeight = sourceImage.Height;
if ((intMaxWidth > 0 && intMaxWidth < intSourceWidth) ||
(intMaxHeight > 0 && intMaxHeight < intSourceHeight))
{
// Resize image:
double aspect = sourceImage.PhysicalDimension.Width / sourceImage.PhysicalDimension.Height;
int newWidth;
int newHeight;
if (intMaxWidth == 0)
{
newWidth = (int)(intMaxHeight * aspect);
newHeight = intMaxHeight;
}
else if (intMaxHeight == 0)
{
newWidth = intMaxWidth;
newHeight = (int)(intMaxWidth / aspect);
}
else if ((intSourceWidth / intMaxWidth) >= (intSourceHeight / intMaxHeight))
{
newWidth = intMaxWidth;
newHeight = (int)(intMaxWidth / aspect);
}
else
{
newWidth = (int)(intMaxHeight * aspect);
newHeight = intMaxHeight;
}
newImage = new Bitmap(newWidth, newHeight);
using (var g = Graphics.FromImage(newImage))
{
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
g.DrawImage(sourceImage, 0, 0, newWidth, newHeight);
}
//Cache thumbnail clone (Disposing a cached image will destroy its cache too)
DataCache.SetCache(cacheKey, newImage.Clone());
}
else //use original width (no maxwidth given or image is narrow enough:
{
newImage = sourceImage;
}
}
else
{
// Get (cloned) cached thumbnail
newImage = (Bitmap)((Bitmap)image).Clone();
iFormat = ImageFormat.Jpeg;
}
// Send image to the browser.
context.Response.ContentType = GetContentType(iFormat);
newImage.Save(context.Response.OutputStream, iFormat);
}
finally
{
// Clean up
if (newImage != null) newImage.Dispose();
if (sourceImage != null) sourceImage.Dispose();
if (sourceImageStream != null) sourceImageStream.Dispose();
}
}
public bool IsReusable
{
get { return false; }
}
static string GetContentType(ImageFormat iFormat)
{
string contentType;
if (iFormat.Guid.Equals(ImageFormat.Jpeg.Guid))
{
contentType = "image/jpeg";
}
else if (iFormat.Guid.Equals(ImageFormat.Gif.Guid))
{
contentType = "image/gif";
}
else if (iFormat.Guid.Equals(ImageFormat.Png.Guid))
{
contentType = "image/png";
}
else if (iFormat.Guid.Equals(ImageFormat.Tiff.Guid))
{
contentType = "image/tiff";
}
else if (iFormat.Guid.Equals(ImageFormat.Bmp.Guid))
{
contentType = "image/x-ms-bmp";
}
else
{
contentType = "image/jpeg";
}
return contentType;
}
}
}