asp.net生成指定大小缩略图

asp.net生成指定大小缩略图,如果图片小于 指定大小,则显示在指定大小画布的中间,如果图片大于 指定大小,则先对图片等比例缩放,在写到指定大小的画板上。可以直接输出到页面,也可以保存为文件。关键代码如下:有哪些可以改进的地方 还请大家提出来,谢谢。

直接复制下来 就可以用:注意引用这三个namespace:

 

 

 

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

 

/// <summary>  

 /// 根据路径读取文件,支持远程文件,本地文件  

  1.    /// </summary>  
  2.    /// <param name="path"></param>  
  3.    /// <returns></returns>  
  4.    private System.Drawing.Image GetImage(string path)  
  5.    {  
  6.        if (path.StartsWith("http"))  
  7.        {  
  8.            System.Net.WebRequest request = System.Net.WebRequest.Create(path);  
  9.            request.Timeout = 10000;  
  10.            System.Net.HttpWebResponse httpresponse = (System.Net.HttpWebResponse)request.GetResponse();  
  11.            Stream s = httpresponse.GetResponseStream();  
  12.  
  13.            return System.Drawing.Image.FromStream(s);  
  14.        }  
  15.        else 
  16.        {  
  17.            return System.Drawing.Image.FromFile(path);  
  18.        }  
  19.    }  
  20.  
  21.    /// <summary>创建规定大小的图像      
  22.    /// </summary>    
  23.    /// <param name="oPath">源图像绝对路径</param>    
  24.    /// <param name="tPath">生成图像绝对路径</param>    
  25.    /// <param name="width">生成图像的宽度</param>    
  26.    /// <param name="height">生成图像的高度</param>    
  27.    public void CreateImageOutput(int width, int height, string oPath)  
  28.    {  
  29.        Bitmap originalBmp = null;// new Bitmap(oPath);  
  30.        originalBmp = new Bitmap(GetImage(oPath));  
  31.        // 源图像在新图像中的位置    
  32.        int left, top;  
  33.  
  34.  
  35.        if (originalBmp.Width <= width && originalBmp.Height <= height)  
  36.        {  
  37.            // 原图像的宽度和高度都小于生成的图片大小    
  38.            left = (int)Math.Round((decimal)(width - originalBmp.Width) / 2);  
  39.            top = (int)Math.Round((decimal)(height - originalBmp.Height) / 2);  
  40.  
  41.  
  42.            // 最终生成的图像    
  43.            Bitmap bmpOut = new Bitmap(width, height);  
  44.            using (Graphics graphics = Graphics.FromImage(bmpOut))  
  45.            {  
  46.                // 设置高质量插值法    
  47.                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  48.                // 清空画布并以白色背景色填充    
  49.                graphics.Clear(Color.White);  
  50.                //加上边框  
  51.                //Pen pen = new Pen(ColorTranslator.FromHtml("#cccccc"));  
  52.                // graphics.DrawRectangle(pen, 0, 0, width - 1, height - 1);  
  53.                // 把源图画到新的画布上    
  54.                graphics.DrawImage(originalBmp, left, top);  
  55.            }  
  56.            // bmpOut.Save(tPath);//保存为文件,tpath 为要保存的路径  
  57.            this.OutputImgToPage(bmpOut);//直接输出到页面  
  58.            bmpOut.Dispose();  
  59.            return;  
  60.        }  
  61.  
  62.  
  63.        // 新图片的宽度和高度,如400*200的图像,想要生成160*120的图且不变形,    
  64.        // 那么生成的图像应该是160*80,然后再把160*80的图像画到160*120的画布上    
  65.        int newWidth, newHeight;  
  66.        if (width * originalBmp.Height < height * originalBmp.Width)  
  67.        {  
  68.            newWidth = width;  
  69.            newHeight = (int)Math.Round((decimal)originalBmp.Height * width / originalBmp.Width);  
  70.            // 缩放成宽度跟预定义的宽度相同的,即left=0,计算top    
  71.            left = 0;  
  72.            top = (int)Math.Round((decimal)(height - newHeight) / 2);  
  73.        }  
  74.        else 
  75.        {  
  76.            newWidth = (int)Math.Round((decimal)originalBmp.Width * height / originalBmp.Height);  
  77.            newHeight = height;  
  78.            // 缩放成高度跟预定义的高度相同的,即top=0,计算left    
  79.            left = (int)Math.Round((decimal)(width - newWidth) / 2);  
  80.            top = 0;  
  81.        }  
  82.  
  83.  
  84.        // 生成按比例缩放的图,如:160*80的图    
  85.        Bitmap bmpOut2 = new Bitmap(newWidth, newHeight);  
  86.        using (Graphics graphics = Graphics.FromImage(bmpOut2))  
  87.        {  
  88.            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  89.            graphics.FillRectangle(Brushes.White, 0, 0, newWidth, newHeight);  
  90.            graphics.DrawImage(originalBmp, 0, 0, newWidth, newHeight);  
  91.        }  
  92.        // 再把该图画到预先定义的宽高的画布上,如160*120    
  93.        Bitmap lastbmp = new Bitmap(width, height);  
  94.        using (Graphics graphics = Graphics.FromImage(lastbmp))  
  95.        {  
  96.            // 设置高质量插值法    
  97.            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  98.            // 清空画布并以白色背景色填充    
  99.            graphics.Clear(Color.White);  
  100.            //加上边框  
  101.            //Pen pen = new Pen(ColorTranslator.FromHtml("#cccccc"));  
  102.            //graphics.DrawRectangle(pen, 0, 0, width - 1, height - 1);  
  103.            // 把源图画到新的画布上    
  104.            graphics.DrawImage(bmpOut2, left, top);  
  105.        }  
  106.        // lastbmp.Save(tPath);//保存为文件,tpath 为要保存的路径  
  107.        this.OutputImgToPage(lastbmp);//直接输出到页面  
  108.        lastbmp.Dispose();  
  109.    }  
  110.  
  111.  
  112.    private void OutputImgToPage(System.Drawing.Bitmap bmp)  
  113.    {  
  114.        //输出到页面  
  115.        MemoryStream ms = new MemoryStream();  
  116.        bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);  
  117.  
  118.        Response.ClearContent(); //需要输出图象信息 要修改HTTP头   
  119.        byte[] buffer = ms.ToArray();  
  120.  
  121.        Response.AddHeader("Content-type""image/jpeg");  
  122.        Response.BinaryWrite(buffer);  
  123.        bmp.Dispose();  
  124.    } 
posted @ 2011-03-28 15:23  cpcpc  阅读(4109)  评论(4编辑  收藏  举报