功能流程
- **縮圖**:原圖縮放至寬或高 300px 內 (保持比例)。
- **裁剪**:使用者選取 90×90 區域 (前端配合 JCrop 等元件)。
- **水晶效果**:疊加陰影漸層、主體漸層、頂部高光漸層,輸出 PNG。
核心程式碼 (ASP.NET WebForms 頁面靜態方法)
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web.UI;
public static class AvatarHelper
{
public static void RenderAvatar(Page page, string srcPath, int rotate, int flip, int shadow,
int offsetX, int offsetY, bool save, string destPath)
{
using (Image srcImg = Image.FromFile(page.Server.MapPath("~/") + srcPath))
{
int w = srcImg.Width, h = srcImg.Height;
if (rotate == 1 || rotate == 3) { int t = w; w = h; h = t; }
if (save) { w = h = 90; }
if (w > 300) w = 300;
if (h > 300) h = 300;
using (Bitmap bmp = new Bitmap(w, h))
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.AntiAlias;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
Color baseColor = Color.DarkTurquoise;
int btnOff = 0, shadowOff = 1;
Rectangle rect = new Rectangle(btnOff, btnOff, w - 1 - btnOff, h - 1 - btnOff);
Rectangle shadowRect = rect; shadowRect.Offset(0, shadowOff);
Rectangle highlightRect = rect; highlightRect.Inflate(-1, -1); highlightRect.Height = 15;
using (GraphicsPath path = RoundedRect(rect, 10))
using (GraphicsPath shadowPath = RoundedRect(shadowRect, 10))
using (GraphicsPath highlightPath = RoundedRect(highlightRect, 10))
using (LinearGradientBrush brushMain = new LinearGradientBrush(
new Point(0, 0), new Point(0, rect.Height + 6), baseColor, Color.White))
using (PathGradientBrush brushShadow = new PathGradientBrush(shadowPath))
using (LinearGradientBrush brushHighlight = new LinearGradientBrush(
highlightRect, Color.FromArgb(255, Color.White), Color.FromArgb(0, Color.White),
LinearGradientMode.Vertical))
{
brushShadow.CenterColor = Color.FromArgb(0, 0, 0);
brushShadow.SurroundColors = new[] { Color.FromArgb(64, 64, 64, 64), Color.FromArgb(64, 128, 128, 128) };
// 1. 陰影
if (shadow > 0) g.FillPath(brushShadow, shadowPath);
// 2. 主體
if (shadow > 0) g.FillPath(brushMain, path);
// 3. 裁剪區域 (可選,防止圖片溢出圓角)
g.SetClip(new RoundedRect(rect, 15), CombineMode.Replace);
// 4. 套用旋轉/翻轉
Image img = srcImg;
if (flip == 1) { img = (Image)img.Clone(); img.RotateFlip(RotateFlipType.RotateNoneFlipX); }
else if (flip == 2) { img = (Image)img.Clone(); img.RotateFlip(RotateFlipType.RotateNoneFlipY); }
if (rotate == 1) { img = (Image)img.Clone(); img.RotateFlip(RotateFlipType.Rotate90FlipNone); }
else if (rotate == 2) { img = (Image)img.Clone(); img.RotateFlip(RotateFlipType.Rotate180FlipNone); }
else if (rotate == 3) { img = (Image)img.Clone(); img.RotateFlip(RotateFlipType.Rotate270FlipNone); }
// 5. 繪製頭像 (offsetX/Y 為負值表示向左上位移)
g.DrawImage(img, -offsetX, -offsetY);
// 6. 頂部高光
if (shadow > 0) g.FillPath(brushHighlight, highlightPath);
}
// 輸出或儲存
using (MemoryStream ms = new MemoryStream())
{
bmp.Save(ms, ImageFormat.Png);
if (save)
{
string fullDest = page.Server.MapPath("~/") + destPath;
bmp.Save(fullDest, ImageFormat.Png);
}
OutputImage(page.Response, ms.ToArray());
}
}
}
}
private static GraphicsPath RoundedRect(Rectangle r, int radius)
{
var gp = new GraphicsPath();
int d = radius * 2;
gp.AddArc(r.X, r.Y, d, d, 180, 90);
gp.AddArc(r.Right - d, r.Y, d, d, 270, 90);
gp.AddArc(r.Right - d, r.Bottom - d, d, d, 0, 90);
gp.AddArc(r.X, r.Bottom - d, d, d, 90, 90);
gp.CloseFigure();
return gp;
}
private static void OutputImage(HttpResponse resp, byte[] pngBytes)
{
resp.Expires = 0;
resp.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
resp.CacheControl = "no-cache";
resp.Clear();
resp.ContentType = "image/png";
resp.BinaryWrite(pngBytes);
resp.End();
}
}
關鍵技術細節
| 技術點 | 說明 |
| 圓角矩形 | `GraphicsPath.AddArc` 四角畫 90° 圓弧組合 |
| 主體漸層 | `LinearGradientBrush` 垂直從基底色到白色 |
| 陰影 | `PathGradientBrush` 中心透明、四周半透明灰,模擬立體 |
| 高光 | 頂部 15px 白色到透明垂直漸層,模擬光澤 |
| 旋轉/翻轉 | `Image.RotateFlip` 列舉,支援 90° 階距與水平/垂直鏡像 |
| 裁剪位移 | `DrawImage` 使用負座標實現「視窗」效果 |
| 資源管理 | 全程 `using` 確保 `Image`、`Graphics`、`Brush`、`Stream` 釋放 |
前端整合建議
- 使用 **JCrop / Cropper.js** 讓使用者在縮圖上選取 90×90 區域,取得 `x, y, w, h` 傳給後端。
- 旋轉/翻轉按鈕對應 `rotate` (0~3) 與 `flip` (0=無, 1=水平, 2=垂直) 參數。
- 啟用「水晶效果」開關對應 `shadow` 參數 (0/1)。
- 儲存後回傳新頭像 URL,前端更新顯示並清除快取 (`?v=timestamp`)。
效能與擴展
- 高併發場景建議改用 **ImageSharp** / **SkiaSharp** (跨平台、無 GDI+ 限制) 或離線佇列處理。
- 可擴充濾鏡:飽和度、對比度、邊框、水印等,同樣在 `Graphics` 階段疊加。
結語
透過 GDI+ 繪圖管線疊加多層漸層與陰影,即可在伺服器端自動生成具備水晶光澤質感的圓角頭像。程式碼結構清晰、資源釋放完備,可直接整合至 ASP.NET WebForms / MVC / Core 專案。
copyright ©
萬盛學電腦網 all rights reserved