nyk0r

Image Scale HTTP Handler

Jan 9th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.17 KB | None | 0 0
  1. <%@ WebHandler Language="C#" Class="ScaledImage" %>
  2.  
  3. using System;
  4. using System.Web;
  5.  
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9.  
  10. // ScaledImage.ashx?image=101&width=200&height=300
  11. public class ScaledImage : IHttpHandler {
  12.     public static class ImageHelper {
  13.         private static void AdjustGraphics(Graphics gr) {
  14.             gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  15.             gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  16.             gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  17.         }
  18.        
  19.         private static bool NeedCrop(int targetWidth, int targetHeight, Image image) {
  20.             double widthFactor = (float)image.Width/targetWidth;
  21.             double heightFacor = (float)image.Height/targetHeight;
  22.  
  23.             return widthFactor - (int) widthFactor >= 0.2 | heightFacor - (int) heightFacor >= 0.2;
  24.         }
  25.  
  26.         private static Image CropImage(Image image, int width, int height) {
  27.             int newWidth = image.Width/width*width;
  28.             int newHeight = image.Height/height*height;
  29.             Rectangle cropRect = new Rectangle((image.Width - newWidth)/2, (image.Height - newHeight)/2, newWidth, newHeight);
  30.             Bitmap result = new Bitmap(newWidth, newHeight);
  31.             using(Graphics gr = Graphics.FromImage(result)) {
  32.                 AdjustGraphics(gr);
  33.                 gr.DrawImage(image, 0, 0, cropRect, GraphicsUnit.Pixel);
  34.             }
  35.             return result;
  36.         }
  37.  
  38.         private static Image ResizeImage(Image image, int width, int height) {
  39.             Bitmap result = new Bitmap(width, height);
  40.             using (Graphics gr = Graphics.FromImage(result)) {
  41.                 AdjustGraphics(gr);
  42.                 gr.DrawImage(image, 0, 0, result.Width, result.Height);
  43.             }
  44.             return result;
  45.         }
  46.        
  47.         public static MemoryStream ResizeImage(string path, int width, int height) {
  48.             using (Image image = Image.FromFile(path)) {
  49.                 Image result = null;
  50.  
  51.                 if (image.Width <= width && image.Height <= height) {
  52.                     result = image;
  53.                 }
  54.                 else {
  55.                     if (NeedCrop(width, height, image)) {
  56.                         using (Image croppedImage = CropImage(image, width, height)) {
  57.                             result = ResizeImage(croppedImage, width, height);
  58.                         }
  59.                     }
  60.                     else {
  61.                         result = ResizeImage(image, width, height);
  62.                     }
  63.                 }
  64.  
  65.                 MemoryStream stream = new MemoryStream();
  66.                 result.Save(stream, ImageFormat.Jpeg);
  67.                 if (result != image) result.Dispose(); // small kludge not to use to much using statements
  68.                 return stream;
  69.             }
  70.         }
  71.     }
  72.  
  73.     public const string IMAGE_PARAM = "image";
  74.     public const string WIDTH_PARAM = "width";
  75.     public const string HEIGHT_PARAM = "height";
  76.    
  77.     public void ProcessRequest (HttpContext context) {
  78.         string path = context.Server.MapPath(string.Format("~/images/image{0}.jpg", ImageId));
  79.         path = File.Exists(path) ? path : context.Server.MapPath("~/images/noimage.jpg");
  80.  
  81.         context.Response.ContentType = "image/jpg";
  82.         using (MemoryStream stream = ImageHelper.ResizeImage(path, TargetWidth, TargetHeight)) {
  83.             stream.WriteTo(context.Response.OutputStream);
  84.         }
  85.     }
  86.    
  87.     private int GetIntParam(string paramName) {
  88.         string value = HttpContext.Current.Request[paramName];
  89.         int result;
  90.         bool success = Int32.TryParse(value, out result);
  91.         return success ? result : -1;
  92.     }
  93.    
  94.     private int ImageId {
  95.         get { return GetIntParam(IMAGE_PARAM); }
  96.     }
  97.  
  98.     private int TargetWidth
  99.     {
  100.         get { return GetIntParam(WIDTH_PARAM); }
  101.     }
  102.  
  103.     private int TargetHeight
  104.     {
  105.         get { return GetIntParam(HEIGHT_PARAM); }
  106.     }
  107.  
  108.     public bool IsReusable {
  109.         get {
  110.             return false;
  111.         }
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment