asp.net生成指定大小縮略圖,如果圖片小於 指定大小,則顯示在指定大小畫布的中間,如果圖片大於 指定大小,則先對圖片等比例縮放,在寫到指定大小的畫板上。可以直接輸出到頁面,也可以保存爲文件。關鍵代碼如下:有哪些可以改進的地方 還請大家提出來,謝謝。
直接複製下來 就可以用:注意引用這三個namespace:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
- /// <summary>
- /// 根據路徑讀取文件,支持遠程文件,本地文件
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- private System.Drawing.Image GetImage(string path)
- {
- if (path.StartsWith("http"))
- {
- System.Net.WebRequest request = System.Net.WebRequest.Create(path);
- request.Timeout = 10000;
- System.Net.HttpWebResponse httpresponse = (System.Net.HttpWebResponse)request.GetResponse();
- Stream s = httpresponse.GetResponseStream();
- return System.Drawing.Image.FromStream(s);
- }
- else
- {
- return System.Drawing.Image.FromFile(path);
- }
- }
- /// <summary>創建規定大小的圖像
- /// </summary>
- /// <param name="oPath">源圖像絕對路徑</param>
- /// <param name="tPath">生成圖像絕對路徑</param>
- /// <param name="width">生成圖像的寬度</param>
- /// <param name="height">生成圖像的高度</param>
- public void CreateImageOutput(int width, int height, string oPath)
- {
- Bitmap originalBmp = null;// new Bitmap(oPath);
- originalBmp = new Bitmap(GetImage(oPath));
- // 源圖像在新圖像中的位置
- int left, top;
- if (originalBmp.Width <= width && originalBmp.Height <= height)
- {
- // 原圖像的寬度和高度都小於生成的圖片大小
- left = (int)Math.Round((decimal)(width - originalBmp.Width) / 2);
- top = (int)Math.Round((decimal)(height - originalBmp.Height) / 2);
- // 最終生成的圖像
- Bitmap bmpOut = new Bitmap(width, height);
- using (Graphics graphics = Graphics.FromImage(bmpOut))
- {
- // 設置高質量插值法
- graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
- // 清空畫布並以白色背景色填充
- graphics.Clear(Color.White);
- //加上邊框
- //Pen pen = new Pen(ColorTranslator.FromHtml("#cccccc"));
- // graphics.DrawRectangle(pen, 0, 0, width - 1, height - 1);
- // 把源圖畫到新的畫布上
- graphics.DrawImage(originalBmp, left, top);
- }
- // bmpOut.Save(tPath);//保存爲文件,tpath 爲要保存的路徑
- this.OutputImgToPage(bmpOut);//直接輸出到頁面
- bmpOut.Dispose();
- return;
- }
- // 新圖片的寬度和高度,如400*200的圖像,想要生成160*120的圖且不變形,
- // 那麼生成的圖像應該是160*80,然後再把160*80的圖像畫到160*120的畫布上
- int newWidth, newHeight;
- if (width * originalBmp.Height < height * originalBmp.Width)
- {
- newWidth = width;
- newHeight = (int)Math.Round((decimal)originalBmp.Height * width / originalBmp.Width);
- // 縮放成寬度跟預定義的寬度相同的,即left=0,計算top
- left = 0;
- top = (int)Math.Round((decimal)(height - newHeight) / 2);
- }
- else
- {
- newWidth = (int)Math.Round((decimal)originalBmp.Width * height / originalBmp.Height);
- newHeight = height;
- // 縮放成高度跟預定義的高度相同的,即top=0,計算left
- left = (int)Math.Round((decimal)(width - newWidth) / 2);
- top = 0;
- }
- // 生成按比例縮放的圖,如:160*80的圖
- Bitmap bmpOut2 = new Bitmap(newWidth, newHeight);
- using (Graphics graphics = Graphics.FromImage(bmpOut2))
- {
- graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
- graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);
- graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight);
- }
- // 再把該圖畫到預先定義的寬高的畫布上,如160*120
- Bitmap lastbmp = new Bitmap(width, height);
- using (Graphics graphics = Graphics.FromImage(lastbmp))
- {
- // 設置高質量插值法
- graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
- // 清空畫布並以白色背景色填充
- graphics.Clear(Color.White);
- //加上邊框
- //Pen pen = new Pen(ColorTranslator.FromHtml("#cccccc"));
- //graphics.DrawRectangle(pen, 0, 0, width - 1, height - 1);
- // 把源圖畫到新的畫布上
- graphics.DrawImage(bmpOut2, left, top);
- }
- // lastbmp.Save(tPath);//保存爲文件,tpath 爲要保存的路徑
- this.OutputImgToPage(lastbmp);//直接輸出到頁面
- lastbmp.Dispose();
- }
- private void OutputImgToPage(System.Drawing.Bitmap bmp)
- {
- //輸出到頁面
- MemoryStream ms = new MemoryStream();
- bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
- Response.ClearContent(); //需要輸出圖象信息 要修改HTTP頭
- byte[] buffer = ms.ToArray();
- Response.AddHeader("Content-type", "p_w_picpath/jpeg");
- Response.BinaryWrite(buffer);
- bmp.Dispose();
- }