1

/**//// <summary>
2
/// 按比例縮放圖檔
3
/// </summary>
4
/// <param name="imgUrl">圖檔的路徑</param>
5
/// <param name="imgHeight">圖檔的高度</param>
6
/// <param name="imgWidth">圖檔的寬度</param>
7
/// <returns></returns>
8
public static string GetImageSize(string imgUrl,int imgHeight,int imgWidth)
9

{
10
string fileName = System.Web.HttpContext.Current.Server.MapPath(imgUrl);
11
string strResult = string.Empty;
12
if(System.IO.File.Exists(fileName) && imgHeight != 0 && imgWidth != 0)
13
14
decimal desWidth;decimal desHeight; //目标寬高
15
System.Drawing.Image objImage = System.Drawing.Image.FromFile(fileName);
16
decimal radioAct = (decimal)objImage.Width/(decimal)objImage.Height; //原始圖檔的寬高比
17
decimal radioLoc = (decimal)imgWidth/(decimal)imgHeight; //圖檔位的寬高比
18
if(radioAct > radioLoc) //原始圖檔比圖檔位寬
19
20
decimal dcmZoom = (decimal)imgWidth/(decimal)objImage.Width;
21
desHeight = objImage.Height*dcmZoom;
22
desWidth = imgWidth;
23
}
24
else
25
26
decimal dcmZoom = (decimal)imgHeight/(decimal)objImage.Height;
27
desWidth = objImage.Width*dcmZoom;
28
desHeight = imgHeight;
29
30
objImage.Dispose(); //釋放資源
31
strResult = "width=\"" + Convert.ToString((int)desWidth) + "\" height=\""
32
+ Convert.ToString((int)desHeight) + "\" ";
33
34
return strResult;
35
本文轉自高海東部落格園部落格,原文連結:http://www.cnblogs.com/ghd258/archive/2005/11/07/270447.html,如需轉載請自行聯系原作者