由於最近開發的項目中需要對上傳的大圖片做裁剪、縮放處理。所以整理了一下。特此記錄。
方法:
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+中發生一般性錯誤。”的錯誤提示。所以這里一定要注意寫全。
相關文章參考:
原創文章,轉載請注明出處。