[翻譯]ASP.NET MVC 3 開發的20個秘訣(十二)[20 Recipes for Programming MVC 3]:縮放圖片尺寸建立縮圖

來源:互聯網
上載者:User

議題

使用者上傳到網站上的大多數的圖片都是大尺寸的照片,通常在使用者想看完整圖片之前網站會展示出這些圖片或照片的縮圖。

解決方案

使用以下的類來調整上傳的圖片檔案的寬和高:FileStream,Image,Bitmap和Graphics。

討論

在下面的樣本中,將修改和重組之前建立的FileUpload類。建立一個新的稱為“ResizeImage”的方法來調整圖片大小。調整之後的圖片檔案將被儲存到源檔案儲存體檔案夾的子檔案夾“Thumbnails”中。同時也要修改DeleteFile方法,添加同時刪除原始映像和縮圖,並且為了避免重複代碼要建立一個新的刪除功能的方法。下面顯示變化部分的類代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace MvcApplication4.Utils
{
public static class FileUpload
{
public static char DirSeparator =
System.IO.Path.DirectorySeparatorChar;
public static string FilesPath = "Content" +
DirSeparator + "Uploads" + DirSeparator;
public static string UploadFile(HttpPostedFileBase file)
{
...
// 儲存縮圖
ResizeImage(file, 150, 100);
...
}
public static void DeleteFile(string fileName)
{
// 如果沒有指定檔案名稱就什麼都不做
if (fileName.Length == 0) return;
// 設定刪除路徑
string path = FilesPath + DirSeparator + fileName;
string thumbPath = FilesPath + DirSeparator +
"Thumbnails" + DirSeparator + fileName;
RemoveFile(path);
RemoveFile(thumbPath);
}
private static void RemoveFile(string path)
{
// 檢查檔案是否存在
if (File.Exists(Path.GetFullPath(path)))
{
// 刪除檔案
File.Delete(Path.GetFullPath(path));
}
}
public static void ResizeImage(HttpPostedFileBase file,
int width, int height)
{
string thumbnailDirectory =
String.Format(@"{0}{1}{2}", FilesPath,
DirSeparator, "Thumbnails");
// 檢查目標檔案夾是否存在
if (!Directory.Exists(thumbnailDirectory))
{
// 假如檔案夾不存在就建立它
Directory.CreateDirectory(thumbnailDirectory);
}
// 設定縮圖儲存路徑
string imagePath =
String.Format(@"{0}{1}{2}", thumbnailDirectory,
DirSeparator, file.FileName);
// 將檔案流儲存到磁碟
FileStream stream = new FileStream(Path.GetFullPath(
imagePath), FileMode.OpenOrCreate);
// 縮放上傳的檔案
Image OrigImage = Image.FromStream(file.InputStream);
// 建立縮圖對象
Bitmap TempBitmap = new Bitmap(width, height);
// 建立縮圖畫質
Graphics NewImage = Graphics.FromImage(TempBitmap);
NewImage.CompositingQuality =
CompositingQuality.HighQuality;
NewImage.SmoothingMode =
SmoothingMode.HighQuality;
NewImage.InterpolationMode =
InterpolationMode.HighQualityBicubic;
// 建立Rectangle對象進行繪製
Rectangle imageRectangle = new Rectangle(0, 0,
width, height);
NewImage.DrawImage(OrigImage, imageRectangle);
// 儲存縮圖
TempBitmap.Save(stream, OrigImage.RawFormat);
// 釋放資源
NewImage.Dispose();
TempBitmap.Dispose();
OrigImage.Dispose();
stream.Close();
stream.Dispose();
}
}
}

在上面的樣本中,我們做了許多修改,特別建立ResizeImage方法。首先,判斷“Thumbnails”檔案夾是否存在以及建立功能。接下來,會建立一個新的FileStream對象,並將編輯後的圖片儲存到“Thumbnails”檔案夾中。 

從提交的InputStream建立原始圖片對象。然後基於縮圖的尺寸建立縮圖的Bitmap位元影像執行個體。然後基於這個Bitmap對象建立新的Graphics對象,然後設定映像的畫質、平滑度、插值模式,如果不設定這些值,縮圖會因為很像素化和比例變形變的很難看。 

將這些值都設定完畢,建立一個原始大小的Recangle對象,將這個對象縮放後繪製到之前建立的Graphics對象中,這時才實際調整尺寸。最後儲存Bitmap對象,釋放所有資源。

參考

FileStream Image Bitmap Graphics 原書地址 書籍原始碼

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.