在asp.net中,上傳圖片功能或者是經常使用的,生成縮略圖也是經常使用的。baidu或者google,c#的方法也是不少的,可是一用卻發現縮略圖不清晰啊,縮略圖片太大之類的事情,下面是我在處理圖片上的代碼,效果不錯,因此拿出來分享,(效果能達到一些繪圖軟件的效果)html
代碼以下:算法
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(HtmlInputFile 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 = 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 //繪製縮略圖 http://www.cnblogs.com/sosoft/ 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 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....c#
下面把調用代碼也一塊兒C上來服務器
1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3 <html xmlns="http://www.w3.org/1999/xhtml" > 4 <head runat="server"> 5 <title>圖片上傳-柯樂義</title> 6 </head> 7 <body> 8 <form id="form1" runat="server"> 9 <div> 10 <input id="File1" runat="server" type="file" /></div><asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> 11 </form> 12 </body> 13 </html>
1 protected void Button1_Click(object sender, EventArgs e) 2 { 3 string a = this.UpLoadImage(this.File1, "UpLoad/", "thumb_", 118, 118); 4 }
這樣就會在你的UpLoad文件夾下多出兩張圖片,一張是原圖,一張是縮略圖。app
提供一個更好的算法,因爲沒有時間去測試和調試,僅供參考asp.net
即,在第一步等比例縮小的時候,能夠分屢次,即把原圖到上面代碼的中間圖以百分比縮小,測試
例如:原圖爲500*500 我要縮略成100*80,上面代碼程序會先繪製一張100*100的中間圖,再在這圖片上繪製100*80的,this
在繪製100*100中間圖以前若是先繪300*300的中間圖,再在300*300的基礎上再繪100*100而後再繪100*80這樣會比我上面的代碼效果更好,圖片更清晰,即中間圖越多,效果越好,你們能夠去試試。google