asp.net生成指定大小縮略圖

asp.net生成指定大小縮略圖,如果圖片小於 指定大小,則顯示在指定大小畫布的中間,如果圖片大於 指定大小,則先對圖片等比例縮放,在寫到指定大小的畫板上。可以直接輸出到頁面,也可以保存爲文件。關鍵代碼如下:有哪些可以改進的地方 還請大家提出來,謝謝。

直接複製下來 就可以用:注意引用這三個namespace:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

  1. /// <summary>  
  2.    /// 根據路徑讀取文件,支持遠程文件,本地文件  
  3.    /// </summary>  
  4.    /// <param name="path"></param>  
  5.    /// <returns></returns>  
  6.    private System.Drawing.Image GetImage(string path)  
  7.    {  
  8.        if (path.StartsWith("http"))  
  9.        {  
  10.            System.Net.WebRequest request = System.Net.WebRequest.Create(path);  
  11.            request.Timeout = 10000;  
  12.            System.Net.HttpWebResponse httpresponse = (System.Net.HttpWebResponse)request.GetResponse();  
  13.            Stream s = httpresponse.GetResponseStream();  
  14.  
  15.            return System.Drawing.Image.FromStream(s);  
  16.        }  
  17.        else 
  18.        {  
  19.            return System.Drawing.Image.FromFile(path);  
  20.        }  
  21.    }  
  22.  
  23.    /// <summary>創建規定大小的圖像      
  24.    /// </summary>    
  25.    /// <param name="oPath">源圖像絕對路徑</param>    
  26.    /// <param name="tPath">生成圖像絕對路徑</param>    
  27.    /// <param name="width">生成圖像的寬度</param>    
  28.    /// <param name="height">生成圖像的高度</param>    
  29.    public void CreateImageOutput(int width, int height, string oPath)  
  30.    {  
  31.        Bitmap originalBmp = null;// new Bitmap(oPath);  
  32.        originalBmp = new Bitmap(GetImage(oPath));  
  33.        // 源圖像在新圖像中的位置    
  34.        int left, top;  
  35.  
  36.  
  37.        if (originalBmp.Width <= width && originalBmp.Height <= height)  
  38.        {  
  39.            // 原圖像的寬度和高度都小於生成的圖片大小    
  40.            left = (int)Math.Round((decimal)(width - originalBmp.Width) / 2);  
  41.            top = (int)Math.Round((decimal)(height - originalBmp.Height) / 2);  
  42.  
  43.  
  44.            // 最終生成的圖像    
  45.            Bitmap bmpOut = new Bitmap(width, height);  
  46.            using (Graphics graphics = Graphics.FromImage(bmpOut))  
  47.            {  
  48.                // 設置高質量插值法    
  49.                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  50.                // 清空畫布並以白色背景色填充    
  51.                graphics.Clear(Color.White);  
  52.                //加上邊框  
  53.                //Pen pen = new Pen(ColorTranslator.FromHtml("#cccccc"));  
  54.                // graphics.DrawRectangle(pen, 0, 0, width - 1, height - 1);  
  55.                // 把源圖畫到新的畫布上    
  56.                graphics.DrawImage(originalBmp, left, top);  
  57.            }  
  58.            // bmpOut.Save(tPath);//保存爲文件,tpath 爲要保存的路徑  
  59.            this.OutputImgToPage(bmpOut);//直接輸出到頁面  
  60.            bmpOut.Dispose();  
  61.            return;  
  62.        }  
  63.  
  64.  
  65.        // 新圖片的寬度和高度,如400*200的圖像,想要生成160*120的圖且不變形,    
  66.        // 那麼生成的圖像應該是160*80,然後再把160*80的圖像畫到160*120的畫布上    
  67.        int newWidth, newHeight;  
  68.        if (width * originalBmp.Height < height * originalBmp.Width)  
  69.        {  
  70.            newWidth = width;  
  71.            newHeight = (int)Math.Round((decimal)originalBmp.Height * width / originalBmp.Width);  
  72.            // 縮放成寬度跟預定義的寬度相同的,即left=0,計算top    
  73.            left = 0;  
  74.            top = (int)Math.Round((decimal)(height - newHeight) / 2);  
  75.        }  
  76.        else 
  77.        {  
  78.            newWidth = (int)Math.Round((decimal)originalBmp.Width * height / originalBmp.Height);  
  79.            newHeight = height;  
  80.            // 縮放成高度跟預定義的高度相同的,即top=0,計算left    
  81.            left = (int)Math.Round((decimal)(width - newWidth) / 2);  
  82.            top = 0;  
  83.        }  
  84.  
  85.  
  86.        // 生成按比例縮放的圖,如:160*80的圖    
  87.        Bitmap bmpOut2 = new Bitmap(newWidth, newHeight);  
  88.        using (Graphics graphics = Graphics.FromImage(bmpOut2))  
  89.        {  
  90.            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  91.            graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);  
  92.            graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight);  
  93.        }  
  94.        // 再把該圖畫到預先定義的寬高的畫布上,如160*120    
  95.        Bitmap lastbmp = new Bitmap(width, height);  
  96.        using (Graphics graphics = Graphics.FromImage(lastbmp))  
  97.        {  
  98.            // 設置高質量插值法    
  99.            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  100.            // 清空畫布並以白色背景色填充    
  101.            graphics.Clear(Color.White);  
  102.            //加上邊框  
  103.            //Pen pen = new Pen(ColorTranslator.FromHtml("#cccccc"));  
  104.            //graphics.DrawRectangle(pen, 0, 0, width - 1, height - 1);  
  105.            // 把源圖畫到新的畫布上    
  106.            graphics.DrawImage(bmpOut2, left, top);  
  107.        }  
  108.        // lastbmp.Save(tPath);//保存爲文件,tpath 爲要保存的路徑  
  109.        this.OutputImgToPage(lastbmp);//直接輸出到頁面  
  110.        lastbmp.Dispose();  
  111.    }  
  112.  
  113.  
  114.    private void OutputImgToPage(System.Drawing.Bitmap bmp)  
  115.    {  
  116.        //輸出到頁面  
  117.        MemoryStream ms = new MemoryStream();  
  118.        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);  
  119.  
  120.        Response.ClearContent(); //需要輸出圖象信息 要修改HTTP頭   
  121.        byte[] buffer = ms.ToArray();  
  122.  
  123.        Response.AddHeader("Content-type""p_w_picpath/jpeg");  
  124.        Response.BinaryWrite(buffer);  
  125.        bmp.Dispose();  
  126.    } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章