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.    }