Image handler
链接:http://www.codeproject.com/KB/aspnet/ImageHandler.aspx
1<%@ WebHandler Language="C#" Class="ImageHandler" %>
2
3using System;
4using System.Drawing;
5using System.Drawing.Imaging;
6using System.IO;
7using System.Web;
8using System.Web.Caching;
9
10public class ImageHandler : IHttpHandler
11{
12 public int _width;
13 public int _height;
14 public static string noImageUrl = @"images\no_photo.jpg";
15 public string imageURL;
16
17
18 public void ProcessRequest(HttpContext context)
19 {
20 Bitmap bitOutput;
21
22 if (context.Cache[("ImageQueryURL-" + context.Request.QueryString.ToString())] !=
23
24null)
25 {
26 bitOutput = (Bitmap)context.Cache[("ImageQueryURL-" +
27
28context.Request.QueryString.ToString())];
29 }
30 else
31 {
32 Bitmap bitInput = GetImage(context);
33
34 bitInput = RotateFlipImage(context, bitInput);
35
36 if (SetHeightWidth(context, bitInput))
37 { bitOutput = ResizeImage(bitInput, _width, _height); }
38 else { bitOutput = bitInput; }
39
40 context.Response.ContentType = "image/jpeg";
41 bitOutput.Save(context.Response.OutputStream,
42
43System.Drawing.Imaging.ImageFormat.Jpeg);
44
45 context.Cache.Insert(("ImageQueryURL-" + context.Request.QueryString.ToString
46
47()), bitOutput, new CacheDependency(imageURL), Cache.NoAbsoluteExpiration,
48
49TimeSpan.FromHours(8), System.Web.Caching.CacheItemPriority.BelowNormal, null);
50 }
51
52 context.Response.ContentType = "image/jpeg";
53 bitOutput.Save(context.Response.OutputStream,
54
55System.Drawing.Imaging.ImageFormat.Jpeg);
56 return;
57 }
58
59
60 /// <summary>
61 /// Get the image requested via the query string.
62 /// </summary>
63 /// <param name="context"></param>
64 /// <returns>Return the requested image or the "no image" default if it does not
65
66exist.</returns>
67 public Bitmap GetImage(HttpContext context)
68 {
69 if (context.Cache[("ImagePath-" + context.Request.QueryString["image"])] == null)
70 {
71 string appPath = context.Server.MapPath(context.Request.ApplicationPath) +
72
73Path.DirectorySeparatorChar;
74
75 if (String.IsNullOrEmpty(context.Request.QueryString["image"]))
76 {
77 appPath += noImageUrl;
78 }
79 else
80 {
81 if (System.IO.File.Exists((appPath + context.Request.QueryString
82
83["image"])))
84 {
85 appPath += context.Request.QueryString["image"];
86 }
87 else
88 {
89 appPath += noImageUrl;
90 }
91 }
92
93 Bitmap bitOutput;
94 imageURL = appPath;
95
96
97 bitOutput = new Bitmap(appPath);
98 context.Cache.Insert(("ImagePath-" + context.Request.QueryString["image"]),
99
100bitOutput, new CacheDependency(imageURL), Cache.NoAbsoluteExpiration, TimeSpan.FromHours
101
102(8), System.Web.Caching.CacheItemPriority.BelowNormal, null);
103 return bitOutput;
104 }
105 else
106 {
107 return (Bitmap)context.Cache[("ImagePath-" + context.Request.QueryString
108
109["image"])];
110 }
111 }
112
113
114 /// <summary>
115 /// Set the height and width of the handler class.
116 /// </summary>
117 /// <param name="context">The context to get the query string parameters, typically
118
119current context.</param>
120 /// <param name="bitInput">The bitmap that determines the </param>
121 /// <returns>True if image needs to be resized, false if original dimensions can be
122
123kept.</returns>
124 public bool SetHeightWidth(HttpContext context, Bitmap bitInput)
125 {
126 double inputRatio = Convert.ToDouble(bitInput.Width) / Convert.ToDouble
127
128(bitInput.Height);
129
130 if (!(String.IsNullOrEmpty(context.Request["width"])) && !(String.IsNullOrEmpty
131
132(context.Request["height"])))
133 {
134 _width = Int32.Parse(context.Request["width"]);
135 _height = Int32.Parse(context.Request["height"]);
136 return true;
137 }
138 else if (!(String.IsNullOrEmpty(context.Request["width"])))
139 {
140 _width = Int32.Parse(context.Request["width"]);
141 _height = Convert.ToInt32( (_width / inputRatio));
142 return true;
143 }
144 else if (!(String.IsNullOrEmpty(context.Request["height"])))
145 {
146 _height = Int32.Parse(context.Request["height"]);
147 _width = Convert.ToInt32((_height * inputRatio));
148 return true;
149 }
150 else
151 {
152 _height = bitInput.Height;
153 _width = bitInput.Width;
154 return false;
155 }
156 }
157
158 /// <summary>
159 /// Flip or rotate the bitmap according to the query string parameters.
160 /// </summary>
161 /// <param name="context">The context of the query string parameters.</param>
162 /// <param name="bitInput">The bitmap to be flipped or rotated.</param>
163 /// <returns>The bitmap after it has been flipped or rotated.</returns>
164 public Bitmap RotateFlipImage(HttpContext context, Bitmap bitInput)
165 {
166 Bitmap bitOut = bitInput;
167
168 if (String.IsNullOrEmpty(context.Request["RotateFlip"]))
169 {
170 return bitInput;
171 }
172 else if (context.Request["RotateFlip"] == "Rotate180flipnone")
173 {
174 bitOut.RotateFlip(RotateFlipType.Rotate180FlipNone);
175 }
176 else if (context.Request["RotateFlip"] == "Rotate180flipx")
177 {
178 bitOut.RotateFlip(RotateFlipType.Rotate180FlipX);
179 }
180 else if (context.Request["RotateFlip"] == "Rotate180flipxy")
181 {
182 bitOut.RotateFlip(RotateFlipType.Rotate180FlipXY);
183 }
184 else if (context.Request["RotateFlip"] == "Rotate180flipy")
185 {
186 bitOut.RotateFlip(RotateFlipType.Rotate180FlipY);
187 }
188 else if (context.Request["RotateFlip"] == "Rotate270flipnone")
189 {
190 bitOut.RotateFlip(RotateFlipType.Rotate270FlipNone);
191 }
192 else if (context.Request["RotateFlip"] == "Rotate270flipx")
193 {
194 bitOut.RotateFlip(RotateFlipType.Rotate270FlipX);
195 }
196 else if (context.Request["RotateFlip"] == "Rotate270FlipXY")
197 {
198 bitOut.RotateFlip(RotateFlipType.Rotate270FlipXY);
199 }
200 else if (context.Request["RotateFlip"] == "Rotate270FlipY")
201 {
202 bitOut.RotateFlip(RotateFlipType.Rotate270FlipY);
203 }
204 else if (context.Request["RotateFlip"] == "Rotate90FlipNone")
205 {
206 bitOut.RotateFlip(RotateFlipType.Rotate90FlipNone);
207 }
208 else if (context.Request["RotateFlip"] == "Rotate90FlipX")
209 {
210 bitOut.RotateFlip(RotateFlipType.Rotate90FlipX);
211 }
212 else if (context.Request["RotateFlip"] == "Rotate90FlipXY")
213 {
214 bitOut.RotateFlip(RotateFlipType.Rotate90FlipXY);
215 }
216 else if (context.Request["RotateFlip"] == "Rotate90FlipY")
217 {
218 bitOut.RotateFlip(RotateFlipType.Rotate90FlipY);
219 }
220 else if (context.Request["RotateFlip"] == "RotateNoneFlipX")
221 {
222 bitOut.RotateFlip(RotateFlipType.RotateNoneFlipX);
223 }
224 else if (context.Request["RotateFlip"] == "RotateNoneFlipXY")
225 {
226 bitOut.RotateFlip(RotateFlipType.RotateNoneFlipXY);
227 }
228 else if (context.Request["RotateFlip"] == "RotateNoneFlipY")
229 {
230 bitOut.RotateFlip(RotateFlipType.RotateNoneFlipY);
231 }
232 else { return bitInput; }
233
234 return bitOut;
235 }
236
237
238 /// <summary>
239 /// Resizes bitmap using high quality algorithms.
240 /// </summary>
241 /// <param name="originalBitmap"></param>
242 /// <param name="newWidth">The width of the returned bitmap.</param>
243 /// <param name="newHeight">The height of the returned bitmap.</param>
244 /// <returns>Resized bitmap.</returns>
245 public static Bitmap ResizeImage(Bitmap originalBitmap, int newWidth, int newHeight)
246 {
247 Bitmap inputBitmap = originalBitmap;
248 Bitmap resizedBitmap = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
249
250 Graphics g = Graphics.FromImage(resizedBitmap);
251 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
252 g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
253 g.InterpolationMode =
254
255System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
256 Rectangle rectangle = new Rectangle(0, 0, newWidth, newHeight);
257 g.DrawImage(inputBitmap, rectangle, 0, 0, inputBitmap.Width, inputBitmap.Height,
258
259GraphicsUnit.Pixel);
260 g.Dispose();
261
262 return resizedBitmap;
263 }
264
265
266 public bool IsReusable
267 {
268 get
269 {
270 return true;
271 }
272 }
273}
2
3using System;
4using System.Drawing;
5using System.Drawing.Imaging;
6using System.IO;
7using System.Web;
8using System.Web.Caching;
9
10public class ImageHandler : IHttpHandler
11{
12 public int _width;
13 public int _height;
14 public static string noImageUrl = @"images\no_photo.jpg";
15 public string imageURL;
16
17
18 public void ProcessRequest(HttpContext context)
19 {
20 Bitmap bitOutput;
21
22 if (context.Cache[("ImageQueryURL-" + context.Request.QueryString.ToString())] !=
23
24null)
25 {
26 bitOutput = (Bitmap)context.Cache[("ImageQueryURL-" +
27
28context.Request.QueryString.ToString())];
29 }
30 else
31 {
32 Bitmap bitInput = GetImage(context);
33
34 bitInput = RotateFlipImage(context, bitInput);
35
36 if (SetHeightWidth(context, bitInput))
37 { bitOutput = ResizeImage(bitInput, _width, _height); }
38 else { bitOutput = bitInput; }
39
40 context.Response.ContentType = "image/jpeg";
41 bitOutput.Save(context.Response.OutputStream,
42
43System.Drawing.Imaging.ImageFormat.Jpeg);
44
45 context.Cache.Insert(("ImageQueryURL-" + context.Request.QueryString.ToString
46
47()), bitOutput, new CacheDependency(imageURL), Cache.NoAbsoluteExpiration,
48
49TimeSpan.FromHours(8), System.Web.Caching.CacheItemPriority.BelowNormal, null);
50 }
51
52 context.Response.ContentType = "image/jpeg";
53 bitOutput.Save(context.Response.OutputStream,
54
55System.Drawing.Imaging.ImageFormat.Jpeg);
56 return;
57 }
58
59
60 /// <summary>
61 /// Get the image requested via the query string.
62 /// </summary>
63 /// <param name="context"></param>
64 /// <returns>Return the requested image or the "no image" default if it does not
65
66exist.</returns>
67 public Bitmap GetImage(HttpContext context)
68 {
69 if (context.Cache[("ImagePath-" + context.Request.QueryString["image"])] == null)
70 {
71 string appPath = context.Server.MapPath(context.Request.ApplicationPath) +
72
73Path.DirectorySeparatorChar;
74
75 if (String.IsNullOrEmpty(context.Request.QueryString["image"]))
76 {
77 appPath += noImageUrl;
78 }
79 else
80 {
81 if (System.IO.File.Exists((appPath + context.Request.QueryString
82
83["image"])))
84 {
85 appPath += context.Request.QueryString["image"];
86 }
87 else
88 {
89 appPath += noImageUrl;
90 }
91 }
92
93 Bitmap bitOutput;
94 imageURL = appPath;
95
96
97 bitOutput = new Bitmap(appPath);
98 context.Cache.Insert(("ImagePath-" + context.Request.QueryString["image"]),
99
100bitOutput, new CacheDependency(imageURL), Cache.NoAbsoluteExpiration, TimeSpan.FromHours
101
102(8), System.Web.Caching.CacheItemPriority.BelowNormal, null);
103 return bitOutput;
104 }
105 else
106 {
107 return (Bitmap)context.Cache[("ImagePath-" + context.Request.QueryString
108
109["image"])];
110 }
111 }
112
113
114 /// <summary>
115 /// Set the height and width of the handler class.
116 /// </summary>
117 /// <param name="context">The context to get the query string parameters, typically
118
119current context.</param>
120 /// <param name="bitInput">The bitmap that determines the </param>
121 /// <returns>True if image needs to be resized, false if original dimensions can be
122
123kept.</returns>
124 public bool SetHeightWidth(HttpContext context, Bitmap bitInput)
125 {
126 double inputRatio = Convert.ToDouble(bitInput.Width) / Convert.ToDouble
127
128(bitInput.Height);
129
130 if (!(String.IsNullOrEmpty(context.Request["width"])) && !(String.IsNullOrEmpty
131
132(context.Request["height"])))
133 {
134 _width = Int32.Parse(context.Request["width"]);
135 _height = Int32.Parse(context.Request["height"]);
136 return true;
137 }
138 else if (!(String.IsNullOrEmpty(context.Request["width"])))
139 {
140 _width = Int32.Parse(context.Request["width"]);
141 _height = Convert.ToInt32( (_width / inputRatio));
142 return true;
143 }
144 else if (!(String.IsNullOrEmpty(context.Request["height"])))
145 {
146 _height = Int32.Parse(context.Request["height"]);
147 _width = Convert.ToInt32((_height * inputRatio));
148 return true;
149 }
150 else
151 {
152 _height = bitInput.Height;
153 _width = bitInput.Width;
154 return false;
155 }
156 }
157
158 /// <summary>
159 /// Flip or rotate the bitmap according to the query string parameters.
160 /// </summary>
161 /// <param name="context">The context of the query string parameters.</param>
162 /// <param name="bitInput">The bitmap to be flipped or rotated.</param>
163 /// <returns>The bitmap after it has been flipped or rotated.</returns>
164 public Bitmap RotateFlipImage(HttpContext context, Bitmap bitInput)
165 {
166 Bitmap bitOut = bitInput;
167
168 if (String.IsNullOrEmpty(context.Request["RotateFlip"]))
169 {
170 return bitInput;
171 }
172 else if (context.Request["RotateFlip"] == "Rotate180flipnone")
173 {
174 bitOut.RotateFlip(RotateFlipType.Rotate180FlipNone);
175 }
176 else if (context.Request["RotateFlip"] == "Rotate180flipx")
177 {
178 bitOut.RotateFlip(RotateFlipType.Rotate180FlipX);
179 }
180 else if (context.Request["RotateFlip"] == "Rotate180flipxy")
181 {
182 bitOut.RotateFlip(RotateFlipType.Rotate180FlipXY);
183 }
184 else if (context.Request["RotateFlip"] == "Rotate180flipy")
185 {
186 bitOut.RotateFlip(RotateFlipType.Rotate180FlipY);
187 }
188 else if (context.Request["RotateFlip"] == "Rotate270flipnone")
189 {
190 bitOut.RotateFlip(RotateFlipType.Rotate270FlipNone);
191 }
192 else if (context.Request["RotateFlip"] == "Rotate270flipx")
193 {
194 bitOut.RotateFlip(RotateFlipType.Rotate270FlipX);
195 }
196 else if (context.Request["RotateFlip"] == "Rotate270FlipXY")
197 {
198 bitOut.RotateFlip(RotateFlipType.Rotate270FlipXY);
199 }
200 else if (context.Request["RotateFlip"] == "Rotate270FlipY")
201 {
202 bitOut.RotateFlip(RotateFlipType.Rotate270FlipY);
203 }
204 else if (context.Request["RotateFlip"] == "Rotate90FlipNone")
205 {
206 bitOut.RotateFlip(RotateFlipType.Rotate90FlipNone);
207 }
208 else if (context.Request["RotateFlip"] == "Rotate90FlipX")
209 {
210 bitOut.RotateFlip(RotateFlipType.Rotate90FlipX);
211 }
212 else if (context.Request["RotateFlip"] == "Rotate90FlipXY")
213 {
214 bitOut.RotateFlip(RotateFlipType.Rotate90FlipXY);
215 }
216 else if (context.Request["RotateFlip"] == "Rotate90FlipY")
217 {
218 bitOut.RotateFlip(RotateFlipType.Rotate90FlipY);
219 }
220 else if (context.Request["RotateFlip"] == "RotateNoneFlipX")
221 {
222 bitOut.RotateFlip(RotateFlipType.RotateNoneFlipX);
223 }
224 else if (context.Request["RotateFlip"] == "RotateNoneFlipXY")
225 {
226 bitOut.RotateFlip(RotateFlipType.RotateNoneFlipXY);
227 }
228 else if (context.Request["RotateFlip"] == "RotateNoneFlipY")
229 {
230 bitOut.RotateFlip(RotateFlipType.RotateNoneFlipY);
231 }
232 else { return bitInput; }
233
234 return bitOut;
235 }
236
237
238 /// <summary>
239 /// Resizes bitmap using high quality algorithms.
240 /// </summary>
241 /// <param name="originalBitmap"></param>
242 /// <param name="newWidth">The width of the returned bitmap.</param>
243 /// <param name="newHeight">The height of the returned bitmap.</param>
244 /// <returns>Resized bitmap.</returns>
245 public static Bitmap ResizeImage(Bitmap originalBitmap, int newWidth, int newHeight)
246 {
247 Bitmap inputBitmap = originalBitmap;
248 Bitmap resizedBitmap = new Bitmap(newWidth, newHeight, PixelFormat.Format24bppRgb);
249
250 Graphics g = Graphics.FromImage(resizedBitmap);
251 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
252 g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
253 g.InterpolationMode =
254
255System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
256 Rectangle rectangle = new Rectangle(0, 0, newWidth, newHeight);
257 g.DrawImage(inputBitmap, rectangle, 0, 0, inputBitmap.Width, inputBitmap.Height,
258
259GraphicsUnit.Pixel);
260 g.Dispose();
261
262 return resizedBitmap;
263 }
264
265
266 public bool IsReusable
267 {
268 get
269 {
270 return true;
271 }
272 }
273}