缩略图
代码链接:缩略图
个人代码:
html页面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <title></title> 5 </head> 6 <body> 7 <form action="upload.ashx" method="post" enctype="multipart/form-data"> 8 <!--这个文件名就是为了演示一下客户端向服务器提交数据时文本框中的内容与文件内容如何一起发送到服务器--> 9 请选择文件: 10 <input type="file" name="file1" value="" /> 11 <input type="submit" value="上传" /> 12 </form> 13 </body> 14 </html>
process处理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ WebHandler Language="C#" Class="upload" %> 2 3 using System; 4 using System.Web; 5 using System.Drawing; 6 7 public class upload : IHttpHandler { 8 9 public void ProcessRequest (HttpContext context) { 10 context.Response.ContentType = "text/plain"; 11 12 //1.接收用户上传的文件 13 HttpPostedFile file = context.Request.Files[0]; 14 //判断文件后缀名,文件的ContentType是不是以"image"开头 15 //file.FileName,file.ContentType="image"; 16 17 //2.根据用户上传的文件创建一个Image对象,这个对象是用户上传的原图的对象 18 using (Image imgBig=Image.FromStream(file.InputStream)) 19 { 20 int bWidth=imgBig.Width; 21 int bHeight=imgBig.Height; 22 //3.再创建一个小图片(Image)对象,这个是一个缩略图。 23 using (Image imgSmall=new Bitmap(200,200 * bHeight / bWidth)) 24 { 25 //4.基于小图片创建一个"画布" 26 using (Graphics g=Graphics.FromImage(imgSmall)) 27 { 28 //5.把大图片"画"到小图片上 29 g.DrawImage(imgBig,0,0,imgSmall.Width,imgSmall.Height); 30 } 31 //6.保存大图片,保存小图片 32 imgBig.Save(context.Request.MapPath(Guid.NewGuid().ToString()+"大图_.jpg")); 33 imgSmall.Save(context.Request.MapPath(Guid.NewGuid().ToString()+"小图_.jpg")); 34 context.Response.Write("ok"); 35 } 36 } 37 } 38 39 public bool IsReusable { 40 get { 41 return false; 42 } 43 } 44 45 }
示例代码(网上):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ WebHandler Language="C#" Class="ShangChuanShengChengSuoLuetu" %> 2 3 using System; 4 using System.Web; 5 using System.Drawing; 6 7 public class ShangChuanShengChengSuoLuetu : IHttpHandler 8 { 9 10 public void ProcessRequest(HttpContext context) 11 { 12 context.Response.ContentType = "text/plain"; 13 //1.获取用户上传的文件 14 HttpPostedFile file = context.Request.Files[0]; 15 string ext = System.IO.Path.GetExtension(file.FileName); 16 if (file.ContentType.StartsWith("image") && ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp") 17 { 18 //生成缩略图 19 20 //1.创建原图 21 using (Image img = Image.FromStream(file.InputStream)) 22 { 23 //2.获得上传图片的高、宽 24 int height = img.Height; 25 int width = img.Width; 26 //3.设置要生成的缩略图的宽 27 int smallWidth = 100; 28 int samllHeight = (int)((height * 1.0 / width) * smallWidth); 29 //1.创建一个缩略图 30 using (System.Drawing.Image suoluetu = new System.Drawing.Bitmap(smallWidth, samllHeight)) 31 { 32 //基于缩略图创建一个画布 33 using (Graphics g = Graphics.FromImage(suoluetu)) 34 { 35 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 36 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 37 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 38 //清除 imgThumb 图的 背景色,然后 设置为 透明背景色 39 g.Clear(Color.Transparent); 40 //把原图画到缩略图上 41 g.DrawImage(img,new Rectangle(0, 0, smallWidth, samllHeight), new Rectangle(0, 0, width, height), GraphicsUnit.Pixel); 42 } 43 suoluetu.Save(context.Request.MapPath("xiaotu_" + Guid.NewGuid().ToString() + ".jpg")); 44 img.Save(context.Request.MapPath("yuanshitu_" + Guid.NewGuid().ToString() + ".jpg")); 45 } 46 } 47 context.Response.Write("OK"); 48 } 49 else 50 { 51 context.Response.Write("Failed"); 52 } 53 54 } 55 56 public bool IsReusable 57 { 58 get 59 { 60 return false; 61 } 62 } 63 64 }