C#图片裁剪高级应用

from http://space.e800.com.cn/hunter01

需求源自一个相册功能,通常用户上传相片后我们都会针对该相片再生成一张缩略图,用于其它页面上的列表显示。

OK,现在来讨论这张缩略图的生成。随便看一下,各大网站基本都是将原图等比缩放来生成缩略图。但完美主义者会发现一些问题,比如显示排版时想让相 册下的相片缩略图列表非常统一、整齐、和美观,比如要求每张缩略图大小固定为120 x 90且不能拉伸变型怎么办?用户上传的相片五花八门,横的、竖的、正方的......,如何做到每张缩略图的大小一致且不拉伸变型呢?

进入主题:图片裁剪高级应用。其实有点标题党了,这项功能并无多大技术含量,也就是普通的裁剪加上一些逻辑。之所以标题里加了高级二字,是因为觉得 这段代码比GOOGLE里搜索“C#图片裁剪”的大多数结果要高级那么一点点,代码中也包含了C#图片裁剪的一些基础知识,希望对入门的朋友有所帮助。

我考虑的逻辑首先根据目标缩略图比例,以图片中心作为裁剪中心最大范围的对原图进行裁剪,然后对裁剪结果等比缩放,以下示例为本地代码处理结果:
1、原图:256 x 192,裁剪要求:100 x 100

-->

2、原图:256 x 192,裁剪要求:90 x 120

-->

3、原图:256 x 192,裁剪要求:120 x 90

-->

4、原图:146 x 256,裁剪要求:100 x 100

-->

5、原图:146 x 256,裁剪要求:90 x 120

-->

6、原图:146 x 256,裁剪要求:120 x 90

-->

 

源代码:

#region 固定模版裁剪并缩放
///
/// 上传图片(以Post方式获取源文件)
/// 按模版比例最大范围的裁剪图片并缩放至模版尺寸
///

/// 原图HttpPostedFile对象
/// 模版宽(单位:px)
/// 模版高(单位:px)
/// 保存路径
public static void UploadImage(System.Web.HttpPostedFile postedFile, int templateWidth, int templateHeight, string fileSaveUrl)
{
//从文件获取原始图片,并使用流中嵌入的颜色管理信息
System.Drawing.Image initImage = System.Drawing.Image.FromStream(postedFile.InputStream, true);

//原图宽高均小于模版,不作处理,直接保存
if (initImage.Width <= templateWidth && initImage.Height <= templateHeight)
{
initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
//模版的宽高比例
double templateRate = double.Parse(templateWidth.ToString()) / templateHeight;
//原图片的宽高比例
double initRate = double.Parse(initImage.Width.ToString()) / initImage.Height;

//原图与模版比例相等,直接缩放
if (templateRate == initRate)
{
//按模版大小生成最终图片
System.Drawing.Image templateImage = new System.Drawing.Bitmap(templateWidth, templateHeight);
System.Drawing.Graphics templateG
= System.Drawing.Graphics.FromImage(templateImage);
templateG.InterpolationMode
= System.Drawing.Drawing2D.InterpolationMode.High;
templateG.SmoothingMode
= System.Drawing.Drawing2D.SmoothingMode.HighQuality;
templateG.Clear(Color.White);
templateG.DrawImage(initImage,
new System.Drawing.Rectangle(0, 0, templateWidth, templateHeight), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
//原图与模版比例不等,裁剪后缩放
else
{
//裁剪对象
System.Drawing.Image pickedImage = null;
System.Drawing.Graphics pickedG
= null;

//定位
Rectangle fromR = new Rectangle(0, 0, 0, 0);//原图裁剪定位
Rectangle toR = new Rectangle(0, 0, 0, 0);//目标定位

//宽为标准进行裁剪
if (templateRate > initRate)
{
//裁剪对象实例化
pickedImage = new System.Drawing.Bitmap(initImage.Width, int.Parse(Math.Floor(initImage.Width / templateRate).ToString()));
pickedG
= System.Drawing.Graphics.FromImage(pickedImage);

//裁剪源定位
fromR.X = 0;
fromR.Y
= int.Parse(Math.Floor((initImage.Height - initImage.Width / templateRate) / 2).ToString());
fromR.Width
= initImage.Width;
fromR.Height
= int.Parse(Math.Floor(initImage.Width / templateRate).ToString());

//裁剪目标定位
toR.X = 0;
toR.Y
= 0;
toR.Width
= initImage.Width;
toR.Height
= int.Parse(Math.Floor(initImage.Width / templateRate).ToString());
}
//高为标准进行裁剪
else
{
pickedImage
= new System.Drawing.Bitmap(int.Parse(Math.Floor(initImage.Height * templateRate).ToString()), initImage.Height);
pickedG
= System.Drawing.Graphics.FromImage(pickedImage);

fromR.X
= int.Parse(Math.Floor((initImage.Width - initImage.Height * templateRate) / 2).ToString());
fromR.Y
= 0;
fromR.Width
= int.Parse(Math.Floor(initImage.Height * templateRate).ToString());
fromR.Height
= initImage.Height;

toR.X
= 0;
toR.Y
= 0;
toR.Width
= int.Parse(Math.Floor(initImage.Height * templateRate).ToString());
toR.Height
= initImage.Height;
}

//设置质量
pickedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode
= System.Drawing.Drawing2D.SmoothingMode.HighQuality;

//裁剪
pickedG.DrawImage(initImage, toR, fromR, System.Drawing.GraphicsUnit.Pixel);

//按模版大小生成最终图片
System.Drawing.Image templateImage = new System.Drawing.Bitmap(templateWidth, templateHeight);
System.Drawing.Graphics templateG
= System.Drawing.Graphics.FromImage(templateImage);
templateG.InterpolationMode
= System.Drawing.Drawing2D.InterpolationMode.High;
templateG.SmoothingMode
= System.Drawing.Drawing2D.SmoothingMode.HighQuality;
templateG.Clear(Color.White);
templateG.DrawImage(pickedImage,
new System.Drawing.Rectangle(0, 0, templateWidth, templateHeight), new System.Drawing.Rectangle(0, 0, pickedImage.Width, pickedImage.Height), System.Drawing.GraphicsUnit.Pixel);
templateImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);

//释放资源
templateG.Dispose();
templateImage.Dispose();

pickedG.Dispose();
pickedImage.Dispose();
}
}

//释放资源
initImage.Dispose();
}
#endregion

posted on 2009-12-17 14:13  Elgin  阅读(513)  评论(0编辑  收藏  举报

导航