網站常需生成縮略圖。.NET `System.Drawing` 提供完整繪圖功能,封裝通用方法支援四種模式:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public static class ImageHelper
{
///
/// 生成高品質縮略圖
///
/// 源圖實體路徑
/// 縮略圖儲存實體路徑
/// 目標寬度
/// 目標高度
/// 模式: HW(強制縮放) W(寬優先) H(高優先) Cut(裁切置中)
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
using (Image originalImage = Image.FromFile(originalImagePath))
{
int toWidth = width;
int toHeight = height;
int x = 0, y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
// 計算繪製來源矩形與目標尺寸
switch (mode.ToUpper())
{
case "HW": // 強制縮放
break;
case "W": // 寬優先
toHeight = originalImage.Height * width / originalImage.Width;
break;
case "H": // 高優先
toWidth = originalImage.Width * height / originalImage.Height;
break;
case "CUT": // 裁切置中
double srcRatio = (double)originalImage.Width / originalImage.Height;
double dstRatio = (double)width / height;
if (srcRatio > dstRatio) // 來源較寬,裁切左右
{
oh = originalImage.Height;
ow = (int)(originalImage.Height * dstRatio);
x = (originalImage.Width - ow) / 2;
}
else // 來源較高,裁切上下
{
ow = originalImage.Width;
oh = (int)(originalImage.Width / dstRatio);
y = (originalImage.Height - oh) / 2;
}
break;
default:
throw new ArgumentException("不支援的模式: " + mode);
}
// 建立目標位圖
using (Bitmap bitmap = new Bitmap(toWidth, toHeight))
using (Graphics g = Graphics.FromImage(bitmap))
{
// 高品質設定
g.InterpolationMode = InterpolationMode.HighQualityBicubic; // 雙三次插值
g.SmoothingMode = SmoothingMode.HighQuality;
g.CompositingQuality = CompositingQuality.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
// 透明背景 (對 PNG 重要)
g.Clear(Color.Transparent);
// 繪製指定來源區域到目標矩形
g.DrawImage(originalImage,
new Rectangle(0, 0, toWidth, toHeight),
new Rectangle(x, y, ow, oh),
GraphicsUnit.Pixel);
// 確保目錄存在
string dir = Path.GetDirectoryName(thumbnailPath);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
// 儲存 (預設 JPEG,可依副檔名決定格式)
ImageFormat format = GetImageFormat(thumbnailPath);
bitmap.Save(thumbnailPath, format);
}
}
}
private static ImageFormat GetImageFormat(string path)
{
string ext = Path.GetExtension(path).ToLower();
return ext switch
{
".png" => ImageFormat.Png,
".gif" => ImageFormat.Gif,
".bmp" => ImageFormat.Bmp,
".tiff" => ImageFormat.Tiff,
_ => ImageFormat.Jpeg
};
}
}
| 設定 | 作用 | 建議值 |
|---|---|---|
| InterpolationMode | 縮放插值算法 | HighQualityBicubic (雙三次,最佳品質) |
| SmoothingMode | 向量圖形平滑度 | HighQuality |
| CompositingQuality | 合成品質 | HighQuality |
| PixelOffsetMode | 像素偏移修正 | HighQuality |
| DrawImage 來源/目標矩形 | 精確控制裁切與縮放區域 | GraphicsUnit.Pixel 確保像素精度 |
// 1. 固定 200x200 強制縮放
ImageHelper.MakeThumbnail(@"C:\Images\photo.jpg", @"C:\Thumbs\photo_hw.jpg", 200, 200, "HW");
// 2. 寬 300,高自適應
ImageHelper.MakeThumbnail(@"C:\Images\photo.jpg", @"C:\Thumbs\photo_w.jpg", 300, 0, "W");
// 3. 高 150,寬自適應
ImageHelper.MakeThumbnail(@"C:\Images\photo.jpg", @"C:\Thumbs\photo_h.jpg", 0, 150, "H");
// 4. 裁切置中 150x150 (適合頭像、商品列表)
ImageHelper.MakeThumbnail(@"C:\Images\photo.jpg", @"C:\Thumbs\photo_cut.jpg", 150, 150, "Cut");
封裝 `MakeThumbnail` 靜態方法,支援四種縮放模式、高品質插值設定、自動格式判斷與資源正確釋放,是 ASP.NET 專案處理縮略圖的標準工具類。生產環境大量圖片處理建議改用非同步佇列 + 專用圖片服務 (如 ImageSharp) 避免阻塞 Web 執行緒。