由于最近开发的项目中需要对上传的大图片做裁剪、缩放处理。所以整理了一下。特此记录。
方法:
public class ImageHandler
{
/// <summary>
/// 对上传的图片进行等比缩放
/// </summary>
/// http://www.cnblogs.com/babycool
/// <param name="fromFile">获取文件流Stream</param>
/// <param name="fileSaveUrl">缩略图保存完整路径</param>
/// <param name="targetWidth">模板宽度</param>
/// <param name="targetHeight">模板高度</param>
public static void ZoomPic(System.IO.Stream fromFile, string fileSaveUrl, System.Double targetWidth, System.Double targetHeight)
{
//原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true);
//原图宽高均小于模版,不作处理,直接保存
if (initImage.Width <= targetWidth && initImage.Height <= targetHeight)
{
//保存
initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
//缩略图宽、高计算
double newWidth = initImage.Width;
double newHeight = initImage.Height;
//宽大于高或宽等于高(横图或正方)
if (initImage.Width > initImage.Height || initImage.Width == initImage.Height)
{
//如果宽大于模版
if (initImage.Width > targetWidth)
{
//宽按模版,高按比例缩放
newWidth = targetWidth;
newHeight = initImage.Height * (targetWidth / initImage.Width);
}
}
//高大于宽(竖图)
else
{
//如果高大于模版
if (initImage.Height > targetHeight)
{
//高按模版,宽按比例缩放
newHeight = targetHeight;
newWidth = initImage.Width * (targetHeight / initImage.Height);
}
}
//生成新图
//新建一个bmp图片
System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
//新建一个画板
System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
//设置质量
newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//置背景色
newG.Clear(Color.White);
//画图
newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
//保存缩略图
newImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
//释放资源
newG.Dispose();
newImage.Dispose();
initImage.Dispose();
}
}
}
/// <summary>
/// 对上传的图片进行等比缩放
/// </summary>
/// http://www.cnblogs.com/babycool
/// <param name="fromFile">获取文件流Stream</param>
/// <param name="fileSaveUrl">缩略图保存完整路径</param>
/// <param name="targetWidth">模板宽度</param>
/// <param name="targetHeight">模板高度</param>
public static void ZoomPic(System.IO.Stream fromFile, string fileSaveUrl, System.Double targetWidth, System.Double targetHeight)
{
//原始图片(获取原始图片创建对象,并使用流中嵌入的颜色管理信息)
System.Drawing.Image initImage = System.Drawing.Image.FromStream(fromFile, true);
//原图宽高均小于模版,不作处理,直接保存
if (initImage.Width <= targetWidth && initImage.Height <= targetHeight)
{
//保存
initImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
}
else
{
//缩略图宽、高计算
double newWidth = initImage.Width;
double newHeight = initImage.Height;
//宽大于高或宽等于高(横图或正方)
if (initImage.Width > initImage.Height || initImage.Width == initImage.Height)
{
//如果宽大于模版
if (initImage.Width > targetWidth)
{
//宽按模版,高按比例缩放
newWidth = targetWidth;
newHeight = initImage.Height * (targetWidth / initImage.Width);
}
}
//高大于宽(竖图)
else
{
//如果高大于模版
if (initImage.Height > targetHeight)
{
//高按模版,宽按比例缩放
newHeight = targetHeight;
newWidth = initImage.Width * (targetHeight / initImage.Height);
}
}
//生成新图
//新建一个bmp图片
System.Drawing.Image newImage = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
//新建一个画板
System.Drawing.Graphics newG = System.Drawing.Graphics.FromImage(newImage);
//设置质量
newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
newG.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//置背景色
newG.Clear(Color.White);
//画图
newG.DrawImage(initImage, new System.Drawing.Rectangle(0, 0, newImage.Width, newImage.Height), new System.Drawing.Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
//保存缩略图
newImage.Save(fileSaveUrl, System.Drawing.Imaging.ImageFormat.Jpeg);
//释放资源
newG.Dispose();
newImage.Dispose();
initImage.Dispose();
}
}
}
调用:
//接收上传后的文件
HttpPostedFile file = context.Request.Files["Filedata"];
//处理图片
ImageHandler.ZoomPic(file.InputStream, uploadPath + file.FileName, 435,600);
注意:ZoomPic方法中的第二个参数“fileSaveUrl”是缩略图保存的完整路径如“uploadPath\123.jpg”的形式,如果只有目录路径则会报“GDI+中发生一般性错误。”的错误提示。所以这里一定要注意写全。
相关文章参考:
原创文章,转载请注明出处。
本文转自 酷小孩 博客园博客,原文链接:http://www.cnblogs.com/babycool/archive/2012/11/18/2775916.html ,如需转载请自行联系原作者