asp.net上傳圖片生成縮圖

iDotNetSpace發表於2010-09-07

在asp.net中,上傳圖片功能或者是常用的,生成縮圖也是常用的。baidu或者google,c#的方法也是很多的,但是一用卻發現縮圖不清晰啊,縮圖片太大之類的事情,下面是我在處理圖片上的程式碼,效果不錯,所以拿出來分享,(效果能達到一些繪圖軟體的效果)

程式碼如下:

001 ///  
002     /// asp.net上傳圖片並生成縮圖 
003     ///  
004     /// HtmlInputFile控制元件 
005     /// 儲存的路徑,些為相對伺服器路徑的下的資料夾 
006     /// 縮圖的thumb 
007     /// 生成縮圖的寬度 
008     /// 生成縮圖的高度 
009     /// 縮圖名稱 
010     public string UpLoadImage(HtmlInputFile upImage, string sSavePath, string sThumbExtension, int intThumbWidth, int intThumbHeight) 
011     { 
012         string sThumbFile = ""
013         string sFilename = "";        
014         if (upImage.PostedFile != null
015         { 
016             HttpPostedFile myFile = upImage.PostedFile; 
017             int nFileLen = myFile.ContentLength; 
018             if (nFileLen == 0) 
019                 return "沒有選擇上傳圖片";            
020             //獲取upImage選擇檔案的副檔名 
021             string extendName = System.IO.Path.GetExtension(myFile.FileName).ToLower(); 
022             //判斷是否為圖片格式 
023             if (extendName != ".jpg" && extendName != ".jpge" && extendName != ".gif" && extendName != ".bmp" && extendName != ".png"
024                 return "圖片格式不正確"
025               
026             byte[] myData = new Byte[nFileLen]; 
027             myFile.InputStream.Read(myData, 0, nFileLen);
028             sFilename = System.IO.Path.GetFileName(myFile.FileName); 
029             int file_append = 0; 
030             //檢查當前資料夾下是否有同名圖片,有則在檔名+1 
031             while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename))) 
032             { 
033                 file_append++; 
034                 sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) 
035                     + file_append.ToString() + extendName; 
036             } 
037             System.IO.FileStream newFile 
038                 = new System.IO.FileStream(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename), 
039                 System.IO.FileMode.Create, System.IO.FileAccess.Write); 
040             newFile.Write(myData, 0, myData.Length); 
041             newFile.Close();
042             //以上為上傳原圖
043             try 
044             { 
045                 //原圖載入 
046                 using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sFilename))) 
047                 { 
048                     //原圖寬度和高度 
049                     int width = sourceImage.Width; 
050                     int height = sourceImage.Height; 
051                     int smallWidth; 
052                     int smallHeight;
053                     //獲取第一張繪製圖的大小,(比較 原圖的寬/縮圖的寬 和 原圖的高/縮圖的高) 
054                     if (((decimal)width) / height <= ((decimal)intThumbWidth) / intThumbHeight) 
055                     { 
056                         smallWidth = intThumbWidth; 
057                         smallHeight = intThumbWidth * height / width; 
058                     } 
059                     else 
060                     { 
061                         smallWidth = intThumbHeight * width / height; 
062                         smallHeight = intThumbHeight; 
063                     }
064                     //判斷縮圖在當前資料夾下是否同名稱檔案存在 
065                     file_append = 0; 
066                     sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + extendName;
067                     while (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(sSavePath + sThumbFile))) 
068                     { 
069                         file_append++; 
070                         sThumbFile = sThumbExtension + System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + 
071                             file_append.ToString() + extendName; 
072                     } 
073                     //縮圖儲存的絕對路徑 
074                     string smallImagePath = System.Web.HttpContext.Current.Server.MapPath(sSavePath) + sThumbFile;
075                     //新建一個圖板,以最小等比例壓縮大小繪製原圖 
076                     using (System.Drawing.Image bitmap = new System.Drawing.Bitmap(smallWidth, smallHeight)) 
077                     { 
078                         //繪製中間圖 
079                         using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap)) 
080                         { 
081                             //高清,平滑 
082                             g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 
083                             g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 
084                             g.Clear(Color.Black); 
085                             g.DrawImage( 
086                             sourceImage, 
087                             new System.Drawing.Rectangle(0, 0, smallWidth, smallHeight), 
088                             new System.Drawing.Rectangle(0, 0, width, height), 
089                             System.Drawing.GraphicsUnit.Pixel 
090                             ); 
091                         } 
092                         //新建一個圖板,以縮圖大小繪製中間圖 
093                         using (System.Drawing.Image bitmap1 = new System.Drawing.Bitmap(intThumbWidth, intThumbHeight)) 
094                         { 
095                             //繪製縮圖 
096                             using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap1)) 
097                             { 
098                                 //高清,平滑 
099                                 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             catch 
113             { 
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     }

 

HtmlInputFile控制元件我想大家都應該知道的,就是input type=file....這個會在後面的一起學asp.net章節中講解

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-672870/,如需轉載,請註明出處,否則將追究法律責任。

相關文章