<%@ Import Namespace="System" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Imaging" %> <%@ Import Namespace="System.Drawing.Drawing2D" %> <%@ Page Language="C#" debug="true" %> <% string filename = Request.QueryString["img"]; string scale_mode = Request.QueryString["scale"]; int width = Int32.Parse(Request.QueryString["width"]); int height = Int32.Parse(Request.QueryString["height"]); if (filename.IndexOf("http://") != -1){ filename = Server.MapPath(VirtualPathUtility.ToAppRelative(new Uri(filename).AbsolutePath)); } else { filename = Server.MapPath("")+"/"+filename; } System.Drawing.Image img_source = System.Drawing.Image.FromFile(filename); int orig_x = 0; 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; }; int dest_x = 0; int dest_y = 0; int dest_w = (int)(orig_w * scale); int dest_h = (int)(orig_h * scale); Bitmap thumb = new Bitmap(dest_w, dest_h, PixelFormat.Format24bppRgb); thumb.SetResolution(img_source.HorizontalResolution, img_source.VerticalResolution); Graphics gr_thumb = Graphics.FromImage(thumb); gr_thumb.InterpolationMode = InterpolationMode.HighQualityBicubic; gr_thumb.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; gr_thumb.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; System.Drawing.Imaging.ImageCodecInfo codec = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()[1]; System.Drawing.Imaging.EncoderParameters eParams = new System.Drawing.Imaging.EncoderParameters(1); eParams.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L); gr_thumb.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); gr_thumb.Dispose(); thumb.Save(Page.Response.OutputStream,codec,eParams); thumb.Dispose(); %>