轉: from : http://www.dotblogs.com.tw/shadow/archive/2011/11/23/59286.aspx
與前一篇QRCODE 有關
:P
產生QRCODE後取圖片
/*要先using以下兩個命名空間*/
protected void Page_Load(object sender, EventArgs e) |
this.saveThumbPic(srcImageUrl, 100, Server.MapPath("~/test.jpg")); |
FileStream fs = new FileStream(Server.MapPath("~/test.jpg"), FileMode.Open); |
byte[] file = new byte[fs.Length]; |
fs.Read(file, 0, file.Length); |
Response.AddHeader("content-disposition", "attachment; filename=test.jpg");//強制下載 |
Response.ContentType = "image/jpg"; |
Response.BinaryWrite(file); |
/// <param name="strUrl">圖片的Url路徑</param> |
/// <returns>回傳 System.Drawing.Image物件</returns> |
public System.Drawing.Image getImageFromURL(string strUrl) |
System.Drawing.Image MyImage = null; |
WebRequest MyWebRequest = WebRequest.Create(strUrl); |
//由 Web Request 取得 Web Response |
WebResponse MyWebResponse = MyWebRequest.GetResponse(); |
//由 Web Response 取得 Stream |
Stream MyStream = MyWebResponse.GetResponseStream(); |
MyImage = System.Drawing.Image.FromStream(MyStream); |
throw new Exception("getImageFromURL(string strUrl)發生例外,可能抓不到網路上的圖片" + strUrl); |
#region [ASP.net程式使用]圖片等比例縮圖後的寬和高像素 |
/// [ASP.net程式使用]取得圖片等比例縮圖後的寬和高像素 |
/// <param name="image">System.Drawing.Image 的物件</param> |
/// <param name="maxPx">寬或高超過多少像素就要縮圖</param> |
/// <returns>回傳int陣列,索引0為縮圖後的寬度、索引1為縮圖後的高度</returns> |
public int[] getThumbPic_WidthAndHeight(System.Drawing.Image image, int maxPx) |
if (image.Width > maxPx || image.Height > maxPx) |
//如果圖片的寬大於最大值或高大於最大值就往下執行 |
if (image.Width >= image.Height) |
fixHeight = Convert.ToInt32((Convert.ToDouble(maxPx) / Convert.ToDouble(image.Width)) * Convert.ToDouble(image.Height)); |
fixWidth = Convert.ToInt32((Convert.ToDouble(maxPx) / Convert.ToDouble(image.Height)) * Convert.ToDouble(image.Width)); |
fixHeight = image.Height; |
int[] fixWidthAndfixHeight = { fixWidth, fixHeight }; |
return fixWidthAndfixHeight; |
/// <param name="srcImageUrl">來源圖片的Url</param> |
/// <param name="maxPix">超過多少像素就要等比例縮圖</param> |
/// <param name="saveThumbFilePath">縮圖的儲存檔案路徑</param> |
public void saveThumbPic(string srcImageUrl, int maxPix, string saveThumbFilePath) |
System.Drawing.Image.GetThumbnailImageAbort callBack = |
new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); |
System.Drawing.Image image = this.getImageFromURL(srcImageUrl); |
int[] thumbnailScale = this.getThumbPic_WidthAndHeight(image, maxPix); |
System.Drawing.Image smallImage = |
image.GetThumbnailImage(thumbnailScale[0], thumbnailScale[1], callBack, IntPtr.Zero); |
smallImage.Save(saveThumbFilePath); |
private bool ThumbnailCallback() |
#endregion