1、C#生成圖片縮略圖
/// <summary> /// 生成縮略圖:背景顏色可以設置 /// </summary> /// <param name="originalImagePath">源圖路徑(物理路徑)</param> /// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param> /// <param name="width">縮略圖寬度</param> /// <param name="height">縮略圖高度</param> /// <param name="byColor">空白背景填充顏色</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string byColor) { //獲取原始圖片 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); //縮略圖畫布寬高 int towidth = width; int toheight = height; //原始圖片寫入畫布座標和寬高(用來設置裁減溢出部分) int x = 0; int y = 0; int ow = originalImage.Width; int oh = originalImage.Height; //原始圖片畫布,設置寫入縮略圖畫布座標和寬高(用來原始圖片整體寬高縮放) int bg_x = 0; int bg_y = 0; int bg_w = towidth; int bg_h = toheight; //倍數變量 double multiple = 0; //獲取寬長的或是高長與縮略圖的倍數 if (originalImage.Width >= originalImage.Height) { multiple = (double)originalImage.Width / (double)width; } else { multiple = (double)originalImage.Height / (double)height; } //上傳的圖片的寬和高小等於縮略圖 if (ow <= width && oh <= height) { //縮略圖按原始寬高 bg_w = originalImage.Width; bg_h = originalImage.Height; //空白部分用背景色填充 bg_x = Convert.ToInt32(((double)towidth - (double)ow) / 2); bg_y = Convert.ToInt32(((double)toheight - (double)oh) / 2); } //上傳的圖片的寬和高大於縮略圖 else { //寬高按比例縮放 bg_w = Convert.ToInt32((double)originalImage.Width / multiple); bg_h = Convert.ToInt32((double)originalImage.Height / multiple); //空白部分用背景色填充 bg_y = Convert.ToInt32(((double)height - (double)bg_h) / 2); bg_x = Convert.ToInt32(((double)width - (double)bg_w) / 2); } //新建一個bmp圖片,並設置縮略圖大小. System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一個畫板 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //設置高質量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; //設置高質量,低速度呈現平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空畫布並設置背景色 g.Clear(System.Drawing.ColorTranslator.FromHtml(byColor)); //在指定位置並且按指定大小繪製原圖片的指定部分 //第一個System.Drawing.Rectangle是原圖片的畫布座標和寬高,第二個是原圖片寫在畫布上的座標和寬高,最後一個參數是指定數值單位爲像素 g.DrawImage(originalImage, new System.Drawing.Rectangle(bg_x, bg_y, bg_w, bg_h), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel); try { //獲取圖片類型 string fileExtension = System.IO.Path.GetExtension(originalImagePath).ToLower(); //按原圖片類型保存縮略圖片,不按原格式圖片會出現模糊,鋸齒等問題. switch (fileExtension) { case ".gif": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif); break; case ".jpg": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); break; case ".bmp": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp); break; case ".png": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png); break; } } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } } /// <summary> /// 生成縮略圖:無填充背景 /// </summary> /// <param name="originalImagePath">源圖路徑(物理路徑)</param> /// <param name="thumbnailPath">縮略圖路徑(物理路徑)</param> /// <param name="width">縮略圖寬度</param> /// <param name="height">縮略圖高度</param> public static void MakeThumbnail2(string originalImagePath, string thumbnailPath, int width, int height) { //獲取原始圖片 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); //縮略圖畫布寬高 int towidth = width;//縮略圖寬度 int toheight = height;//縮略圖高度 //原始圖片寫入畫布座標和寬高(用來設置裁減溢出部分) int x = 0; int y = 0; int ow = originalImage.Width;//原圖寬度 int oh = originalImage.Height;//原圖高度 //先判斷寬,寬大於指定寬度的話將寬設置爲指定寬度,高度等比例縮放。 if (originalImage.Width > width) { towidth = width; toheight = originalImage.Height * towidth / originalImage.Width; } else { towidth = originalImage.Width; toheight = originalImage.Height; } //再判斷高,高大於指定高度的話將高設置爲指定高度,寬度等比例縮放。 if (toheight > height) { toheight = height; towidth = originalImage.Width * toheight / originalImage.Height; } //新建一個bmp圖片 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一個畫板 Graphics g = System.Drawing.Graphics.FromImage(bitmap); //設置高質量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //設置高質量,低速度呈現平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空畫布並以透明背景色填充 g.Clear(Color.Transparent); //在指定位置並且按指定大小繪製原圖片的指定部分 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); try { //獲取圖片類型 string fileExtension = System.IO.Path.GetExtension(originalImagePath).ToLower(); //按原圖片類型保存縮略圖片,不按原格式圖片會出現模糊,鋸齒等問題. switch (fileExtension) { case ".gif": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif); break; case ".jpg": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); break; case ".bmp": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp); break; case ".png": bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png); break; } } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } }
2、C#等比縮放圖片
protected void Page_Load(object sender, EventArgs e) { //測試 string picUrl = "/upload/1.png"; string picPath = Server.MapPath(picUrl); ChangePicSize(picPath, 800); Response.Write("<img src='" + picUrl + "'>"); } /// <summary> /// 等比縮放圖片 /// </summary> /// <param name="pathImageFrom">圖片地址(服務器絕對路徑)</param> /// <param name="maxSize">寬和高最大值</param> public void ChangePicSize(string pathImageFrom, int maxSize) { System.Drawing.Image imageFrom = null; if (File.Exists(pathImageFrom)) { imageFrom = System.Drawing.Image.FromFile(pathImageFrom); // 源圖寬度及高度 int imageFromWidth = imageFrom.Width; int imageFromHeight = imageFrom.Height; // 生成的縮略圖實際寬度及高度 int bitmapWidth = imageFromWidth; int bitmapHeight = imageFromHeight; if (imageFromWidth > maxSize || imageFromHeight > maxSize) { if (imageFromWidth > imageFromHeight) { bitmapWidth = maxSize; bitmapHeight = imageFromHeight * maxSize / imageFromWidth; } else { bitmapWidth = imageFromWidth * maxSize / imageFromHeight; bitmapHeight = maxSize; } } // 創建畫布 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(bitmapWidth, bitmapHeight); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp); // 用白色清空 g.Clear(System.Drawing.Color.White); // 指定高質量的雙三次插值法。執行預篩選以確保高質量的收縮。此模式可產生質量最高的轉換圖像。 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // 指定高質量、低速度呈現。 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // 在指定位置並且按指定大小繪製指定的 Image 的指定部分。 g.DrawImage(imageFrom, new System.Drawing.Rectangle(0, 0, bitmapWidth, bitmapHeight), new System.Drawing.Rectangle(0, 0, imageFromWidth, imageFromHeight), System.Drawing.GraphicsUnit.Pixel); imageFrom.Dispose(); bmp.Save(pathImageFrom, System.Drawing.Imaging.ImageFormat.Jpeg); //釋放資源 bmp.Dispose(); g.Dispose(); } }