PageRenderTime 37ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/IZWebFileManager/ThumbnailHandler.cs

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