純 .NET Framework 實現,無第三方依賴。三大高頻圖片處理場景,代碼可直接落地項目。
常規縮放會模糊,關鍵在於 InterpolationMode.HighQualityBicubic + CompositingQuality.HighQuality + SmoothingMode.HighQuality 三重保障:
public static Image GetHighQualityThumbnail(Image source, int maxWidth, int maxHeight) {
var destRect = new Rectangle(0, 0, maxWidth, maxHeight);
var destImage = new Bitmap(maxWidth, maxHeight);
destImage.SetResolution(source.HorizontalResolution, source.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage)) {
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes()) {
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(source, destRect, 0, 0, source.Width, source.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
權衡:輸出文件可能比原圖大(因無壓縮),建議縮略圖 ≤ 200px 使用;大圖需配合 Encoder 參數壓縮。
算法:原圖中心為裁剪中心 → 取最大可能裁剪區域 (目標長寬比) → 等比縮放至目標尺寸。
public static Image SmartCrop(Image source, int targetWidth, int targetHeight) {
double targetRatio = (double)targetWidth / targetHeight;
double sourceRatio = (double)source.Width / source.Height;
int cropWidth, cropHeight, cropX, cropY;
if (sourceRatio > targetRatio) { // 原圖更寬 → 裁剪左右
cropHeight = source.Height;
cropWidth = (int)(cropHeight * targetRatio);
cropX = (source.Width - cropWidth) / 2;
cropY = 0;
} else { // 原圖更高 → 裁剪上下
cropWidth = source.Width;
cropHeight = (int)(cropWidth / targetRatio);
cropX = 0;
cropY = (source.Height - cropHeight) / 2;
}
var cropRect = new Rectangle(cropX, cropY, cropWidth, cropHeight);
var result = new Bitmap(targetWidth, targetHeight);
using (var g = Graphics.FromImage(result)) {
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(source, new Rectangle(0, 0, targetWidth, targetHeight), cropRect, GraphicsUnit.Pixel);
}
return result;
}
效果對比(原圖 256×192 → 目標 100×100/90×120/120×90):始終保留主體核心區域,無拉伸變形。
public static Image AddTextWatermark(Image source, string text, Font font, Color color,
float opacity = 0.5f, ContentAlignment alignment = ContentAlignment.BottomRight,
int margin = 10) {
var result = new Bitmap(source.Width, source.Height);
using (var g = Graphics.FromImage(result)) {
g.DrawImage(source, 0, 0);
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
var brush = new SolidBrush(Color.FromArgb((int)(opacity * 255), color));
var textSize = g.MeasureString(text, font);
float x = alignment switch {
ContentAlignment.TopLeft => margin,
ContentAlignment.TopCenter => (source.Width - textSize.Width) / 2,
ContentAlignment.TopRight => source.Width - textSize.Width - margin,
ContentAlignment.MiddleLeft => margin,
ContentAlignment.MiddleCenter => (source.Width - textSize.Width) / 2,
ContentAlignment.MiddleRight => source.Width - textSize.Width - margin,
ContentAlignment.BottomLeft => margin,
ContentAlignment.BottomCenter => (source.Width - textSize.Width) / 2,
_ => source.Width - textSize.Width - margin // BottomRight
};
float y = alignment switch {
ContentAlignment.TopLeft or ContentAlignment.TopCenter or ContentAlignment.TopRight => margin,
ContentAlignment.MiddleLeft or ContentAlignment.MiddleCenter or ContentAlignment.MiddleRight => (source.Height - textSize.Height) / 2,
_ => source.Height - textSize.Height - margin
};
g.DrawString(text, font, brush, x, y);
}
return result;
}
進階擴展:支援圖片水印、對角線平鋪、旋轉文字、陰影效果。
using (var original = Image.FromFile("photo.jpg")) {
// 1. 高清縮略圖
var thumb = GetHighQualityThumbnail(original, 200, 200);
thumb.Save("thumb.jpg", ImageFormat.Jpeg);
// 2. 智能裁剪為正方形頭像
var avatar = SmartCrop(original, 100, 100);
avatar.Save("avatar.jpg", ImageFormat.Jpeg);
// 3. 加水印後保存
var watermarked = AddTextWatermark(original, "© 2024 MyApp",
new Font("Microsoft YaHei", 24), Color.White, 0.4f);
watermarked.Save("watermarked.jpg", ImageFormat.Jpeg);
}
Graphics、Font、Brush 對象Image.GetThumbnailImageAbort 回調防卡死ImageSharp 或 SkiaSharp 獲得跨平台高性能三大技巧覆蓋了 90% 的業務圖片處理需求。核心心法:高質量插值算法 + 幾何變換數學 + GDI+ 繪圖管線控制。代碼已脫敏通用,拿去即用;若有更優雅的實現,歡迎交流切磋。