/IZWebFileManager/ThumbnailHandler.cs
C# | 46 lines | 39 code | 7 blank | 0 comment | 2 complexity | 3710985bf66ac46db3527bc236e74d61 MD5 | raw file
1using System; 2using System.Collections.Generic; 3using System.Text; 4using System.Web; 5using System.Drawing; 6using System.Drawing.Imaging; 7 8namespace IZ.WebFileManager 9{ 10 public class ThumbnailHandler : IHttpHandler 11 { 12 public bool IsReusable 13 { 14 get { return true; } 15 } 16 17 public void ProcessRequest(HttpContext context) 18 { 19 context.Response.ContentType = "image/jpeg"; 20 int size = 92; 21 22 string vPath = HttpUtility.UrlDecode(context.Request.Url.Query.Substring(1)); 23 string path = context.Request.MapPath(vPath); 24 25 using (var original = Image.FromFile(path)) 26 { 27 if (original.Width > size || original.Height > size) 28 { 29 int tw = original.Width > original.Height ? size : (original.Width * size) / original.Height; 30 int th = original.Width > original.Height ? (original.Height * size) / original.Width : size; 31 32 using (var thumb = original.GetThumbnailImage(tw, th, null, IntPtr.Zero)) 33 { 34 thumb.Save(context.Response.OutputStream, ImageFormat.Jpeg); 35 36 } 37 } 38 else 39 { 40 original.Save(context.Response.OutputStream, ImageFormat.Jpeg); 41 } 42 } 43 44 } 45 } 46}