I’m trying to build this class (to use in ASP.NET site) that will Crop Image given custom Width,Height X,Y, then take the result image and Scale it to Custom Width, Height, And save in directory on the server return url of this image.
And i will get these paramerts in 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 Crop Image (width, height, x, y) then Scale the Croped image (scalwidth, scalheight) then save jpg in directory and return url for the image location
So what is the best way to this?
Create a
Generic Handler(i.e. ashx file) in your asp.net website or application, and place the following code inside it. Call it for example “Handler.ashx”.Now, in the browser use:
Handler.ashx?x=100&y=300&w=800&h=500&scalew=160&scaleh=100Code of Handler.ashx file:
EDIT:
Some reference about Generic Handlers:
HTTP Handlers and HTTP Modules Overview
@ WebHandler – how ashx files work.