C # Crop then scales the cropped image

I am trying to build this class (for use on ASP.NET site) that will crop the image based on user width, height X, Y, then take the result image and scale it to user width, height and save to the directory on the server returns the URL- address of this image.

And I will get these parameters in a querystring like this

Default.aspx?x=100&y=300&w=800&h=500&scalew=160&scaleh=100 

So this is what I got so far

    public static Image CustomCrop(int width, int height, int x, int y, int scalwidth, int scalheight)
    {
        try
        {
            Image image = Image.FromFile("Images/none.jpg");
            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            bmp.SetResolution(80, 60);

            Graphics gfx = Graphics.FromImage(bmp);
            gfx.SmoothingMode = SmoothingMode.AntiAlias;
            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gfx.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel);


            return bmp;
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message);
            return null;
        }
    }

I will send these values ​​to Crop Image (width, height, x, y), then scale the cropped image (scalwidth, scalheight), then save the jpg in the directory and return the url for the image location

So what is the best way to do this?

+3
source share
3 answers

Generic Handler (.. ashx) - asp.net . , , "Handler.ashx".

: Handler.ashx?x=100&y=300&w=800&h=500&scalew=160&scaleh=100

Handler.ashx:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public class Handler : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/jpeg";
        int x = int.Parse(context.Request["x"]);
        int y = int.Parse(context.Request["y"]);
        int h = int.Parse(context.Request["h"]);
        int w = int.Parse(context.Request["w"]);
        int scalew = int.Parse(context.Request["scalew"]);
        int scaleh = int.Parse(context.Request["scaleh"]);
        using (Image img = CustomCrop(w, h, x, y, scalew, scaleh))
            img.Save(context.Response.OutputStream,ImageFormat.Jpeg); 
    }

    public static Image CustomCrop(int width, int height, int x, int y, int scalwidth, int scalheight)
    {
        try
        {
            Image image = Image.FromFile("c:\\x.jpg");
            Bitmap bmp = new Bitmap(scalwidth, scalheight, PixelFormat.Format24bppRgb);
            bmp.SetResolution(80, 60);

            Graphics gfx = Graphics.FromImage(bmp);
            gfx.SmoothingMode = SmoothingMode.AntiAlias;
            gfx.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gfx.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gfx.DrawImage(image, new Rectangle(0, 0, scalwidth, scalheight), x, y, width, height, GraphicsUnit.Pixel);


            return bmp;
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message);
            return null;
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}

EDIT:

:

HTTP- HTTP-

@WebHandler - ashx.

+3

A - imageresizing.net, GDI +, WIC, FreeImage, 2009 60 . 10 000 - .

System.Drawing is almost impossible to use correctly. I have documented about 30 traps , and more will follow soon. This can be done safely, but you are not going to do this by copying and pasting the code.

It is better to use a shell than to handle memory management for you from end to end, and avoids all GDI + and ASP.NET errors.

+1
source

All Articles