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

2024-04-11

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

我将在查询字符串中获取这些参数,如下所示

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),然后缩放裁剪图像(缩放宽度、滚动高度),然后将 jpg 保存在目录中并返回图像位置的 url

那么最好的方法是什么?


创建一个Generic Handler(即 ashx 文件)在您的 ASP.NET 网站或应用程序中,并将以下代码放入其中。例如将其称为“Handler.ashx”。

现在,在浏览器中使用:Handler.ashx?x=100&y=300&w=800&h=500&scalew=160&scaleh=100

Code of 处理程序.ashx file:

<%@ 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 模块概述 http://msdn.microsoft.com/en-us/library/bb398986.aspx

@Web处理程序 http://msdn.microsoft.com/en-us/library/ms366713.aspx- ashx 文件如何工作。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# 裁剪然后缩放裁剪后的图像 的相关文章

随机推荐