Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@ WebHandler Language="C#" Class="ScaledImage" %>
- using System;
- using System.Web;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Imaging;
- // ScaledImage.ashx?image=101&width=200&height=300
- public class ScaledImage : IHttpHandler {
- public static class ImageHelper {
- private static void AdjustGraphics(Graphics gr) {
- gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
- gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
- gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
- }
- private static bool NeedCrop(int targetWidth, int targetHeight, Image image) {
- double widthFactor = (float)image.Width/targetWidth;
- double heightFacor = (float)image.Height/targetHeight;
- return widthFactor - (int) widthFactor >= 0.2 | heightFacor - (int) heightFacor >= 0.2;
- }
- private static Image CropImage(Image image, int width, int height) {
- int newWidth = image.Width/width*width;
- int newHeight = image.Height/height*height;
- Rectangle cropRect = new Rectangle((image.Width - newWidth)/2, (image.Height - newHeight)/2, newWidth, newHeight);
- Bitmap result = new Bitmap(newWidth, newHeight);
- using(Graphics gr = Graphics.FromImage(result)) {
- AdjustGraphics(gr);
- gr.DrawImage(image, 0, 0, cropRect, GraphicsUnit.Pixel);
- }
- return result;
- }
- private static Image ResizeImage(Image image, int width, int height) {
- Bitmap result = new Bitmap(width, height);
- using (Graphics gr = Graphics.FromImage(result)) {
- AdjustGraphics(gr);
- gr.DrawImage(image, 0, 0, result.Width, result.Height);
- }
- return result;
- }
- public static MemoryStream ResizeImage(string path, int width, int height) {
- using (Image image = Image.FromFile(path)) {
- Image result = null;
- if (image.Width <= width && image.Height <= height) {
- result = image;
- }
- else {
- if (NeedCrop(width, height, image)) {
- using (Image croppedImage = CropImage(image, width, height)) {
- result = ResizeImage(croppedImage, width, height);
- }
- }
- else {
- result = ResizeImage(image, width, height);
- }
- }
- MemoryStream stream = new MemoryStream();
- result.Save(stream, ImageFormat.Jpeg);
- if (result != image) result.Dispose(); // small kludge not to use to much using statements
- return stream;
- }
- }
- }
- public const string IMAGE_PARAM = "image";
- public const string WIDTH_PARAM = "width";
- public const string HEIGHT_PARAM = "height";
- public void ProcessRequest (HttpContext context) {
- string path = context.Server.MapPath(string.Format("~/images/image{0}.jpg", ImageId));
- path = File.Exists(path) ? path : context.Server.MapPath("~/images/noimage.jpg");
- context.Response.ContentType = "image/jpg";
- using (MemoryStream stream = ImageHelper.ResizeImage(path, TargetWidth, TargetHeight)) {
- stream.WriteTo(context.Response.OutputStream);
- }
- }
- private int GetIntParam(string paramName) {
- string value = HttpContext.Current.Request[paramName];
- int result;
- bool success = Int32.TryParse(value, out result);
- return success ? result : -1;
- }
- private int ImageId {
- get { return GetIntParam(IMAGE_PARAM); }
- }
- private int TargetWidth
- {
- get { return GetIntParam(WIDTH_PARAM); }
- }
- private int TargetHeight
- {
- get { return GetIntParam(HEIGHT_PARAM); }
- }
- public bool IsReusable {
- get {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment