<%@ Page Language="C#" debug="true" %> <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Drawing2D" %> <%@ Import Namespace="System.Drawing.Imaging" %> <% string filename = this.Request.QueryString["img"]; string scale_mode = this.Request.QueryString["scale"]; int width = Int32.Parse(this.Request.QueryString["width"]); int height = Int32.Parse(this.Request.QueryString["height"]); if(filename.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) { filename = this.Server.MapPath(VirtualPathUtility.ToAppRelative(new Uri(filename).AbsolutePath)); } else { filename = this.Server.MapPath("") + "/" + filename; } using(System.Drawing.Image img_source = System.Drawing.Image.FromFile(filename)) { const int orig_x = 0; const int orig_y = 0; int orig_w = img_source.Width; int orig_h = img_source.Height; float scale_x = (float)width / orig_w; float scale_y = (float)height / orig_h; float scale = 1; switch(scale_mode.ToLower()) { case "fit": scale = Math.Min(Math.Min(scale_x, scale_y), 1); break; case "crop": scale = Math.Max(scale_x, scale_y); break; case "fill": scale = Math.Max(scale_x, scale_y); break; case "fix": scale = scale_y; break; } const int dest_x = 0; const int dest_y = 0; int dest_w = (int)(orig_w * scale); int dest_h = (int)(orig_h * scale); using(Bitmap thumb = new Bitmap(dest_w, dest_h, PixelFormat.Format24bppRgb)) { thumb.SetResolution(img_source.HorizontalResolution, img_source.VerticalResolution); using(Graphics graphics = Graphics.FromImage(thumb)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.DrawImage(img_source, new Rectangle(dest_x - 1, dest_y - 1, dest_w + 2, dest_h + 2), new Rectangle(orig_x, orig_y, orig_w, orig_h), GraphicsUnit.Pixel); } ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders()[1]; EncoderParameters eParams = new EncoderParameters(1); eParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); thumb.Save(this.Page.Response.OutputStream, codec, eParams); } } %>