[英]C# Crop then scale the Cropped Image
我正在尝试构建这个类(在ASP.NET站点中使用),它将裁剪图像给定自定义宽度,高度X,Y,然后获取结果图像并将其缩放到自定义宽度,高度,并保存在目录上服务器返回此图像的网址。
我会像这样在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)然后缩放Croped图像(scalwidth,scalheight)然后将jpg保存在目录中并返回图像位置的url
那么最好的方法是什么?
在asp.net网站或应用程序中创建一个Generic Handler
(即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;
}
}
}
编辑:
关于通用处理程序的一些参考:
@ WebHandler - ashx文件的工作原理。
好吧,你已经有了工作代码,这确实是其中一种方式 - 你可以添加压缩(例如,JPEG格式会使用有损压缩) - 请参阅这篇文章: http : //www.britishdeveloper.co.uk/2011/ 05 /图像调整大小-裁剪和- compression.html
但是, 我建议不要使用System.Drawing
命名空间 。 根据MSDN文档 ,不支持在ASP.NET应用程序中使用GDI +(即System.Drawing)。 相反,它推荐使用Windows Imaging Components(即从.NET角度使用WPF Imaging - 它在内部使用WIC)。 看到这篇文章将开始使用WPF来裁剪/缩放图像: http : //weblogs.asp.net/bleroy/archive/2009/12/10/resizing-images-from-the-server-using-wpf-wic -instead-的-gdi.aspx
已经存在经过充分测试和验证的开源库 - imageresizing.net ,它支持GDI +,WIC和FreeImage后端。 我在2009年写了它,并从那时起发布了60个新版本。 它已在超过10,000个网站上使用,并为一些非常大的社交网站提供支持。
System.Drawing 几乎不可能正确使用。 我已经记录了大约30个陷阱 ,并且很快就会有更多陷阱 。 它可以安全地完成,但你不会通过复制和粘贴代码来实现它。
使用包装器比从端到端处理内存管理更好,并避免所有GDI +和ASP.NET陷阱。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.