<%@ WebHandler Language="C#" Class="Thumbnail" Debug="false" %> using System; using System.Web; using System.Drawing; using System.IO; using System.Text; using System.Globalization; public class Thumbnail : IHttpHandler { private string imageUrl = ""; private int width = 0; private int height = 0; private int x1 = -1; private int y1 = -1; private int x2 = -1; private int y2 = -1; private string saveFolder = "~/images/resize_buffer"; private ImageResize.ResizeMode resize_mode = ImageResize.ResizeMode.scaleDown; private bool return_path = false; private int cropHeight = 0; public void ProcessRequest(HttpContext context) { if (context.Request.QueryString["help"] != null) { context.Response.ContentType = "text/html"; StringBuilder sb = new StringBuilder(); sb.Append("Οδηγίες χρήσης Thumbnail component."); sb.Append(""); sb.Append("

Οδηγίες χρήσης thumbnail.ashx

"); sb.Append("

Παράμετροι

"); sb.Append("

url

"); sb.Append("

Το url path της εικόνας που θα εμφανίσει.

"); sb.Append("

width, height

"); sb.Append("

Ορίζει ένα τετράγωνο μέσα στο οποίο θα σχεδιάσει την εικόνα χωρίς να ξεφύγει καμία διάσταση μέσα απο αυτό.

"); sb.Append("

width

"); sb.Append("

Αν στείλω μόνο τη width παράμετρο τότε περιορίζει το thumbnail μόνο στο width και αφήνει την εικόνα να πάρει όσο ύψος χρειάζεται.

"); sb.Append("

saveFolder

"); sb.Append("

Αν υπάρχει αυτή η παράμετρος αποθηκεύει την εικόνα που φτιάχνει στο folder αυτό.

"); sb.Append("

Παράδειγμα

"); sb.Append("

thumbnail.ashx?url=/image/mypicture.jpg&width=200&height=150

"); sb.Append(""); context.Response.Write(sb.ToString()); } else { if (!int.TryParse(context.Request["cropHeight"], out cropHeight)) cropHeight = 0; if (cropHeight < 0) cropHeight = 0; int return_path_query_string = 0; int.TryParse(context.Request["return_path"], out return_path_query_string); return_path = (return_path_query_string == 1); int resize_mode_query_string = 0; int.TryParse(context.Request["resize_mode"], out resize_mode_query_string); switch (resize_mode_query_string) { case 0: resize_mode = ImageResize.ResizeMode.scaleDown; break; case 1: resize_mode = ImageResize.ResizeMode.scale; break; default: resize_mode = ImageResize.ResizeMode.scaleDown; break; } saveFolder = context.Request["saveFolder"]; if (saveFolder == string.Empty || saveFolder == null) { saveFolder = "~/images/resize_buffer"; } if (!Directory.Exists(context.Server.MapPath(saveFolder))) { Directory.CreateDirectory(context.Server.MapPath(saveFolder)); } imageUrl = context.Request["url"]; if (imageUrl == null || imageUrl.Length <= 0) { return; } if (!int.TryParse(context.Request["width"], out width)) { width = 0; } else if (!int.TryParse(context.Request["height"], out height)) { height = 0; } string cachedImagePath = null; if (int.TryParse(context.Request["x1"], out x1) && int.TryParse(context.Request["x2"], out x2) && int.TryParse(context.Request["y1"], out y1) && int.TryParse(context.Request["y2"], out y2)) { cachedImagePath = Path.Combine(saveFolder, string.Format("{0}_{1}_{2}_{3}_{4}x{5}_{6}_ch{7}.jpg", x1, y1, x2, y2, width, height, Path.GetFileNameWithoutExtension(imageUrl), cropHeight, ".jpg")); } else { cachedImagePath = Path.Combine(saveFolder, string.Format("{0}x{1}_{2}_ch{3}.jpg", width, height, Path.GetFileNameWithoutExtension(imageUrl), cropHeight, ".jpg")); } if (!File.Exists(context.Server.MapPath(cachedImagePath))) //if (1==1) { Image image = null; if ((width > 0 && height > 0) || (height > 0 && width <= 0)) { image = ImageResize.ResizeImage( imageUrl, width, height, x1, y1, x2, y2, System.Drawing.Drawing2D.CompositingQuality.HighQuality, System.Drawing.Drawing2D.SmoothingMode.HighQuality, System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic, resize_mode); } else { if (width > 0 && height <= 0) { image = ImageResize.ResizeImage( imageUrl, width, x1, y1, x2, y2, System.Drawing.Drawing2D.CompositingQuality.HighQuality, System.Drawing.Drawing2D.SmoothingMode.HighQuality, System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic, resize_mode, cropHeight); } } if (image != null) { image.Save(context.Server.MapPath(cachedImagePath), System.Drawing.Imaging.ImageFormat.Jpeg); } else { cachedImagePath = imageUrl; } if (image != null) image.Dispose(); } string mappedImagePath = context.Server.MapPath(cachedImagePath); if (File.Exists(mappedImagePath)) { DateTime fileLastWriteTime = File.GetLastWriteTime(mappedImagePath); if (return_path) { context.Response.ContentType = "text/plain"; context.Response.Write(cachedImagePath.Replace("\\", "/").Replace("~/", "")); } else { context.Response.ContentType = "image/jpg"; if (!string.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"])) { DateTime serverModDate; DateTime.TryParse(context.Request.Headers["If-Modified-Since"], CultureInfo.GetCultureInfo("en-US"), DateTimeStyles.AdjustToUniversal, out serverModDate); TimeSpan serverModDateTicks = TimeSpan.FromTicks(serverModDate.ToLocalTime().Ticks); TimeSpan fileLastWriteTicks = TimeSpan.FromTicks(fileLastWriteTime.Ticks); if (Math.Truncate(serverModDateTicks.TotalSeconds) < Math.Truncate(fileLastWriteTicks.TotalSeconds)) { SendActualImageFile(context, fileLastWriteTime, Path.GetFileName(imageUrl), cachedImagePath, mappedImagePath); } else { context.Response.Expires = 15000; context.Response.Cache.SetLastModified(fileLastWriteTime); context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetValidUntilExpires(true); context.RewritePath(cachedImagePath); context.Response.AddHeader("content-disposition", "inline; filename=" + Path.GetFileName(imageUrl)); context.Response.StatusCode = 304; context.Response.Status = "304 Not Modified"; } } else { SendActualImageFile(context, fileLastWriteTime, Path.GetFileName(imageUrl), cachedImagePath, mappedImagePath); } } } else { context.Response.StatusCode = 500; // SendActualImageFile(context, DateTime.Now, "DummyGray1x1.png", "/images/DummyGray1x1.png", context.Server.MapPath("/images/DummyGray1x1.png")); } } } private void SendActualImageFile(HttpContext context, DateTime ALastModified, string AFilename, string ARewritePath, string AFilePath) { context.Response.ContentType = "image/jpg"; context.Response.Expires = 15000; context.Response.Cache.SetLastModified(ALastModified); context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetValidUntilExpires(true); context.Response.AddHeader("content-disposition", "inline; filename=" + AFilename); context.RewritePath(ARewritePath); context.Response.TransmitFile(AFilePath); } public bool IsReusable { get { return false; } } }