我正在尝试构建这个类(用于在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),然后缩放裁剪后的图像(scalwidth、scalheight),然后将jpg保存在目录中并返回图像位置的url。那么,最好的方法是什么?