asp.net显示高清缩略图

来源于http://blog.csdn.net/yanghua_kobe/article/details/4733223


如题:

 

添加命名空间如下:

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


-----------------------------------------------------------------------------------------------

 

显示原图:

[c-sharp]  view plain copy print ?
  1. //显示原图  
  2.     protected void btnImage_Click(object sender, EventArgs e)  
  3.     {  
  4.         string filePath = Server.MapPath("~/Up/美女.jpg");  
  5.         //方式一:  
  6.         //Response.ContentType = "image/jpeg";  
  7.         //Response.WriteFile(filePath);  
  8.   
  9.         //方式二:  
  10.         FileStream imageStr = new FileStream(filePath, FileMode.Open);  
  11.         byte[] imageData=new byte[imageStr.Length];  
  12.         imageStr.Read(imageData,0,(int)imageStr.Length);  
  13.         Response.OutputStream.Write(imageData, 0, (int)imageStr.Length);  
  14.           
  15.     }  

 

想要改变的尺寸:

[c-sharp]  view plain copy print ?
  1. private static Size NewSize(int maxWidth, int maxHeight, int Width, int Height)  
  2.     {  
  3.         double w = 0.0;  
  4.         double h = 0.0;  
  5.         double sw = Convert.ToDouble(Width);  
  6.         double sh = Convert.ToDouble(Height);  
  7.         double mw = Convert.ToDouble(maxWidth);  
  8.         double mh = Convert.ToDouble(maxHeight);  
  9.   
  10.         if (sw < mw && sh < mh)//如果maxWidth和maxHeight大于源图像,则缩略图的长和高不变  
  11.         {  
  12.             w = sw;  
  13.             h = sh;  
  14.         }  
  15.         else if ((sw / sh) > (mw / mh))  
  16.         {  
  17.             w = maxWidth;  
  18.             h = (w * sh) / sw;  
  19.         }  
  20.         else  
  21.         {  
  22.             h = maxHeight;  
  23.             w = (h * sw) / sh;  
  24.         }  
  25.         return new Size(Convert.ToInt32(w), Convert.ToInt32(h));  
  26.     }  

 

转化成缩略图:

 

[c-sharp]  view plain copy print ?
  1. //转化成缩略图  
  2.     public void SendSmallImage(string filename, string newfile, int maxHeight, int maxWidth)  
  3.     {  
  4.         System.Drawing.Image img = System.Drawing.Image.FromFile(filename);//源图像的信息  
  5.         System.Drawing.Imaging.ImageFormat thisformat = img.RawFormat; //源图像的格式  
  6.   
  7.         Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height); //返回调整后的图像Width与Height  
  8.         Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);  
  9.         Graphics g = Graphics.FromImage(outBmp);  
  10.         //设置画布的描绘质量(高)  
  11.         g.CompositingQuality = CompositingQuality.HighQuality;  
  12.         g.SmoothingMode = SmoothingMode.HighQuality;  
  13.         g.InterpolationMode = InterpolationMode.HighQualityBicubic;  
  14.         g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);  
  15.         g.Dispose();  
  16.         //以下代码为保存图片时,设置压缩质量  
  17.         EncoderParameters encoderParams = new EncoderParameters();  
  18.         long[] quality = new long[1];  
  19.         quality[0] = 100;  
  20.         EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);  
  21.         encoderParams.Param[0] = encoderParam;  
  22.         //获取包含有关内置图像编码解码器的信息的ImageCodecInfo对象。  
  23.         ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();  
  24.         ImageCodecInfo jpegICI = null;  
  25.         for (int x = 0; x < arrayICI.Length; x++)  
  26.         {  
  27.             if (arrayICI[x].FormatDescription.Equals("JPEG"))  
  28.             {  
  29.                 jpegICI = arrayICI[x];//设置jpeg编码  
  30.                 break;  
  31.             }  
  32.         }  
  33.         if (jpegICI != null)  
  34.         {  
  35.             outBmp.Save(newfile , jpegICI, encoderParams);  
  36.         }  
  37.         else  
  38.         {  
  39.             outBmp.Save(newfile, thisformat);  
  40.         }  
  41.         img.Dispose();  
  42.         outBmp.Dispose();  
  43.     }  

 

页面上是一个FileUpload控件,先上传原图,在显示在页面上,添加一个Image控件,宽,高设为:100×100,用以对比缩略图。

将原图输出缩略为100×100;

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值