首页 > 代码库 > 缩略图制作(能运行,但缩略图生成的有问题,待改进)

缩略图制作(能运行,但缩略图生成的有问题,待改进)

 

首先是前台代码:

1 <asp:FileUpload ID="FileUpload1" runat="server" />2 <asp:Button ID="Button1" runat="server" Text="上传" OnClick ="Button1_Click"/>

 

然后是上传按钮的单击事件(其中上传文件的文件夹,要存在):

1 protected void Button1_Click(object sender, EventArgs e)2 {3     string a = this.UpLoadImage(this.FileUpload1, "UpLoad/", "thumb_", 200, 100);4 }

 

然后是生成缩略图方法(此方法转自:http://blog.csdn.net/ojekleen/article/details/2754255):

  1 /// <summary>  2 /// asp.net上传图片并生成缩略图  3 /// </summary>  4 /// <param name="upImage">HtmlInputFile控件</param>  5 /// <param name="sSavePath">保存的路径,些为相对服务器路径的下的文件夹</param>  6 /// <param name="sThumbExtension">缩略图的thumb</param>  7 /// <param name="intThumbWidth">生成缩略图的宽度</param>  8 /// <param name="intThumbHeight">生成缩略图的高度</param>  9 /// <returns>缩略图名称</returns> 10 public string UpLoadImage(FileUpload upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight) 11 { 12     string sThumbFile = ""; 13     string sFilename = ""; 14     if (upImage.PostedFile != null) 15     { 16         HttpPostedFile myFile = upImage.PostedFile; 17         int nFileLen = myFile.ContentLength; 18         if (nFileLen == 0) 19             return "没有选择上传图片"; 20         //获取upImage选择文件的扩展名 21         string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower(); 22         //判断是否为图片格式 23         if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png") 24             return "图片格式不正确"; 25  26         byte[] myData = http://www.mamicode.com/new Byte[nFileLen]; 27         myFile.InputStream.Read(myData, 0, nFileLen); 28         sFilename = System.IO.Path.GetFileName(myFile.FileName); 29         int file_append = 0; 30         //检查当前文件夹下是否有同名图片,有则在文件名+1 31         while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename))) 32         { 33             file_append++; 34             sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) 35                 + file_append.ToString() + extendName; 36         } 37         System.IO.FileStream newFile 38             = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename), 39             System.IO.FileMode.Create, System.IO.FileAccess.Write); 40         newFile.Write(myData, 0, myData.Length); 41         newFile.Close(); 42         //以上为上传原图 43         try 44         { 45             //原图加载 46             using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename))) 47             { 48                 //原图宽度和高度 49                 int width = sourceImage.Width; 50                 int height = sourceImage.Height; 51                 int smallWidth; 52                 int smallHeight; 53                 //获取第一张绘制图的大小,(比较 原图的宽/缩略图的宽  和 原图的高/缩略图的高) 54                 if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight) 55                 { 56                     smallWidth = intThumbWidth; 57                     smallHeight = intThumbWidth * height / width; 58                 } 59                 else 60                 { 61                     smallWidth = intThumbHeight * width / height; 62                     smallHeight = intThumbHeight; 63                 } 64                 //判断缩略图在当前文件夹下是否同名称文件存在 65                 file_append = 0; 66                 sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName; 67                 while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile))) 68                 { 69                     file_append++; 70                     sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + 71                         file_append.ToString() + extendName; 72                 } 73                 //缩略图保存的绝对路径 74                 string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile; 75                 //新建一个图板,以最小等比例压缩大小绘制原图 76                 using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight)) 77                 { 78                     //绘制中间图 79                     using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) 80                     { 81                         //高清,平滑 82                         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 83                         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 84                         g.Clear(Color.Black); 85                         g.DrawImage( 86                         sourceImage, 87                         new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight), 88                         new System.Drawing.Rectangle(0, 0, width, height), 89                         System.Drawing.GraphicsUnit.Pixel 90                         ); 91                     } 92                     //新建一个图板,以缩略图大小绘制中间图 93                     using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight)) 94                     { 95                         //绘制缩略图 96                         using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1)) 97                         { 98                             //高清,平滑 99                             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;100                             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;101                             g.Clear(Color.Black);102                             int lwidth = (smallWidth - intThumbWidth) / 2;103                             int bheight = (smallHeight - intThumbHeight) / 2;104                             g.DrawImage(bitmap, new Rectangle(0, 0, intThumbWidth, intThumbHeight), lwidth, bheight, intThumbWidth, intThumbHeight, GraphicsUnit.Pixel);105                             g.Dispose();106                             bitmap1.Save(smallImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);107                         }108                     }109                 }110             }111         }112         catch113         {114             //出错则删除115             System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename));116             return "图片格式不正确";117         }118         //返回缩略图名称119         return sThumbFile;120     }121     return "没有选择图片";122 }

 

运行效果:

技术分享

 

最终上传(文件夹要已经存在):

技术分享

 

以上就是缩略图的制作。

 

缩略图制作(能运行,但缩略图生成的有问题,待改进)