C#裁剪然后缩放裁剪后的图像

C#裁剪然后缩放裁剪后的图像,c#,asp.net,image-processing,C#,Asp.net,Image Processing,我正在尝试构建这个类(在ASP.NET站点中使用),该类将在给定自定义宽度、高度X、Y的情况下裁剪图像,然后获取结果图像并将其缩放为自定义宽度、高度,并保存在服务器上的目录中,返回此图像的url 我会像这样在querystring中获取这些参数 Default.aspx?x=100&y=300&w=800&h=500&scalew=160&scaleh=100 这就是我到目前为止得到的 public static Image CustomCro

我正在尝试构建这个类(在ASP.NET站点中使用),该类将在给定自定义宽度、高度X、Y的情况下裁剪图像,然后获取结果图像并将其缩放为自定义宽度、高度,并保存在服务器上的目录中,返回此图像的url

我会像这样在querystring中获取这些参数

Default.aspx?x=100&y=300&w=800&h=500&scalew=160&scaleh=100 
这就是我到目前为止得到的

    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;
        }
    }
我将发送这些值裁剪图像(宽度、高度、x、y),然后缩放交叉图像(scalwidth、scalheight),然后将jpg保存在目录中,并返回图像位置的url


那么最好的方法是什么呢?

在asp.net网站或应用程序中创建一个
通用处理程序(即ashx文件),并将以下代码放在其中。例如,将其称为“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;
        }
    }

}

使用制度;
使用System.Web;
使用系统图;
使用System.Drawing.Drawing2D;
使用系统、绘图、成像;
公共类处理程序:IHttpHandler{
公共void ProcessRequest(HttpContext上下文){
context.Response.ContentType=“image/jpeg”;
intx=int.Parse(context.Request[“x”]);
int y=int.Parse(context.Request[“y”]);
inth=int.Parse(context.Request[“h”]);
intw=int.Parse(context.Request[“w”]);
int scalew=int.Parse(context.Request[“scalew”]);
int scaleh=int.Parse(context.Request[“scaleh”]);
使用(图像img=自定义裁剪(w、h、x、y、scalew、scaleh))
保存(context.Response.OutputStream,ImageFormat.Jpeg);
}
公共静态图像自定义裁剪(int-width、int-height、int-x、int-y、int-scalwidth、int-scalheight)
{
尝试
{
Image=Image.FromFile(“c:\\x.jpg”);
位图bmp=新位图(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;
DrawImage(图像,新矩形(0,0,scalwidth,scalheight),x,y,width,height,GraphicsUnit.Pixel);
返回bmp;
}
捕获(例外情况除外)
{
//MessageBox.Show(例如Message);
返回null;
}
}
公共布尔可重用{
得到{
返回false;
}
}
}
编辑:

有关泛型处理程序的一些参考:


-ashx文件的工作原理。

好吧,您已经有了工作代码,这确实是一种方法-您可以添加压缩(例如,JPEG格式将使用有损压缩)-请参阅本文:

但是,我建议不要使用
System.Drawing
名称空间
。根据,不支持在ASP.NET应用程序中使用GDI+(即System.Drawing)。相反,它建议使用Windows映像组件(即从.NET的角度来看,使用——它在内部使用WIC)。请参阅这篇文章,它将开始使用WPF裁剪/缩放图像:

A,它支持GDI+、WIC和FreeImage后端。我在2009年写的,从那时起已经发布了60个新版本。它在10000多个网站上使用,并为一些非常大型的社交网站提供了动力

系统。图纸几乎不可能正确使用,很快就会有更多。可以安全地执行此操作,但您不会通过复制和粘贴代码来执行此操作


使用包装器比从端到端为您处理内存管理更好,而且可以避免所有GDI+和ASP.NET陷阱。

有人否决了这个问题,但没有解释原因。来吧,人们。。。不要那样做。如果否决票的动机不在评论中,请发表评论,说明为什么你认为这是错误的。。。我觉得这个问题不应该被否决,而是我回答了。不是每个人都知道一切。问题作者在解决自己的问题时确实表现出了努力,他已经有了几乎一半的努力。。。这里的人的问题是这样的。差不多5年后,我仍然在问自己同样的问题。它确实像我想要的那样工作得很好,但似乎有一些性能问题非常严重。因此,我将尝试增强is,谢谢,它应该——它像筛子一样泄漏内存。每个System.Drawing对象都必须包装在using(){}子句中。此外,这将导致外边缘周围有50%的透明边框。好的,在我看来,使用System.Drawing编写了许多组件,但它不受支持,并且在某些情况下会导致运行时错误,我将尝试WPF映像并测试这两个组件