public static Image Resize(System.Drawing.Image image, Resolution targetResolution, ResizeMode resizeMode) { int sourceWidth = image.Width; int sourceHeight = image.Height; int targetWidth = targetResolution.Width; int targetHeight = targetResolution.Height; // Supplied image is landscape, while the target resolution is portait OR // supplied image is in portait, while the target resolution is in landscape. // switch target resolution to match the image. if ((sourceWidth > sourceHeight && targetWidth < targetHeight) || (sourceWidth < sourceHeight && targetWidth > targetHeight)) { targetWidth = targetResolution.Height; targetHeight = targetResolution.Width; } float ratio = 0; float ratioWidth = ((float)targetWidth / (float)sourceWidth); float ratioHeight = ((float)targetHeight / (float)sourceHeight); if (ratioHeight < ratioWidth) ratio = ratioHeight; else ratio = ratioWidth; Bitmap newImage = null; switch (resizeMode) { case ResizeMode.Normal: default: { int destWidth = (int)(sourceWidth * ratio); int destHeight = (int)(sourceHeight * ratio); newImage = new Bitmap(destWidth, destHeight); using (Graphics graphics = Graphics.FromImage(newImage)) { graphics.Clear(Color.Transparent); graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, 0, 0, destWidth, destHeight); } break; } case ResizeMode.Crop: { if (ratioHeight > ratioWidth) ratio = ratioHeight; else ratio = ratioWidth; int destWidth = (int)(sourceWidth * ratio); int destHeight = (int)(sourceHeight * ratio); newImage = new Bitmap(targetWidth, targetHeight); int startX = 0; int startY = 0; if (destWidth > targetWidth) startX = 0 - ((destWidth - targetWidth) / 2); if (destHeight > targetHeight) startY = 0 - ((destHeight - targetHeight) / 2); using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)newImage)) { graphics.Clear(Color.Transparent); graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, startX, startY, destWidth, destHeight); } break; } case ResizeMode.Stretch: { newImage = new Bitmap(targetWidth, targetHeight); using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)newImage)) { graphics.Clear(Color.Transparent); graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, 0, 0, targetWidth, targetHeight); } break; } case ResizeMode.Fill: { newImage = new Bitmap(targetWidth, targetHeight); int destWidth = (int)(sourceWidth * ratio); int destHeight = (int)(sourceHeight * ratio); int startX = 0; int startY = 0; if (destWidth < targetWidth) startX = 0 + ((targetWidth - destWidth) / 2); if (destHeight < targetHeight) startY = 0 + ((targetHeight - destHeight) / 2); newImage = new Bitmap(targetWidth, targetHeight); using (Graphics graphics = Graphics.FromImage((System.Drawing.Image)newImage)) { graphics.Clear(Color.Transparent); graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.DrawImage(image, startX, startY, destWidth, destHeight); } break; } } return (System.Drawing.Image)newImage; }
public void ProcessRequest(HttpContext context) { TimeSpan refresh = new TimeSpan(0, 15, 0); context.Response.Cache.SetExpires(DateTime.Now.Add(refresh)); context.Response.Cache.SetMaxAge(refresh); context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.CacheControl = HttpCacheability.Public.ToString(); context.Response.Cache.SetValidUntilExpires(true); string currentPath = context.Server.MapPath(context.Request.Url.LocalPath); string currentDirectory = Directory.GetParent(currentPath).ToString(); string img = !string.IsNullOrEmpty(context.Request.QueryString["Img"]) ? Directory.GetParent(currentDirectory) + "\\Uploads\\" + context.Request.QueryString["Img"] : currentDirectory + "\\NoImage.png"; string scale = !string.IsNullOrEmpty(context.Request.QueryString["Scale"]) ? context.Request.QueryString["Scale"] : "normal"; //giữ tỉ lệ int width = !string.IsNullOrEmpty(context.Request.QueryString["W"]) ? int.Parse(context.Request.QueryString["W"]) : 100; int height = !string.IsNullOrEmpty(context.Request.QueryString["H"]) ? int.Parse(context.Request.QueryString["H"]) : 100; Resolution aResolution = new Resolution{ Width = width, Height = height }; Image imgToResize = File.Exists(img) ? Image.FromFile(img) : Image.FromFile(currentDirectory + "\\NoImage.png"); switch (scale.ToLower()){ case "normal": imgToResize = Resize(imgToResize, aResolution, ResizeMode.Normal); break; case "crop": imgToResize = Resize(imgToResize, aResolution, ResizeMode.Crop); break; case "fill": imgToResize = Resize(imgToResize, aResolution, ResizeMode.Fill); break; case "stretch": imgToResize = Resize(imgToResize, aResolution, ResizeMode.Stretch); break; } context.Response.ContentType = "image/jpeg"; imgToResize.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); }