ASP.NET讀取網絡或本地圖片顯示

寫這個的緣由是在CSDN看到的兩個問題:
1、抓取網絡圖片,不在本地保存而直接顯示
2、在站點服務器上某個磁盤的文件裏有圖片,想能夠在網站上顯示出來,圖片文件夾不在站點目錄 

 

一、讀取網絡圖片

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5. </head>  
  6. <body>  
  7.     <form id="form1" runat="server">  
  8.     <div>  
  9.         <img src="Handler.ashx?url=http://www.google.com.hk/intl/zh-CN/images/logo_cn.png" mce_src="http://Handler.ashx?url=http://www.google.com.hk/intl/zh-CN/images/logo_cn.png"  
  10.             alt="google logo" />  
  11.     </div>  
  12.     </form>  
  13. </body>  
  14. </html>  
 

 

Handler.ashx

  1. <%@ WebHandler Language="C#" Class="Handler" %>  
  2. using System;  
  3. using System.Web;  
  4. using System.Net;  
  5. using System.Drawing;  
  6. using System.IO;  
  7. public class Handler : IHttpHandler {  
  8.       
  9.     public void ProcessRequest (HttpContext context) {  
  10.         string imgUrl = context.Request["Url"];  
  11.         if (!string.IsNullOrEmpty(imgUrl))  
  12.         {  
  13.             Uri myUri = new Uri(imgUrl);  
  14.             WebRequest webRequest = WebRequest.Create(myUri);  
  15.             WebResponse webResponse = webRequest.GetResponse();  
  16.             Bitmap myImage = new Bitmap(webResponse.GetResponseStream());  
  17.             MemoryStream ms = new MemoryStream();  
  18.             myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);  
  19.             context.Response.ClearContent();  
  20.             context.Response.ContentType = "image/Jpeg";  
  21.             context.Response.BinaryWrite(ms.ToArray());  
  22.         }  
  23.     }  
  24.    
  25.     public bool IsReusable {  
  26.         get {  
  27.             return false;  
  28.         }  
  29.     }  
  30. }  
 

 

二、讀取本地圖片

讀取本地文件,如:d:/1.jpg

  1. <%@ WebHandler Language="C#" Class="Handler2" %>  
  2. using System;  
  3. using System.Web;  
  4. using System.IO;  
  5. using System.Drawing;  
  6. public class Handler2 : IHttpHandler {  
  7.     public void ProcessRequest(HttpContext context)  
  8.     {  
  9.         string path = context.Request.QueryString["path"];  
  10.         if (!string.IsNullOrEmpty(path))  
  11.         {  
  12.             FileStream fs = new FileStream(@path, FileMode.Open, FileAccess.Read);  
  13.             Bitmap myImage = new Bitmap(fs);  
  14.               
  15.             MemoryStream ms = new MemoryStream();  
  16.             myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);  
  17.             context.Response.ClearContent();  
  18.             context.Response.ContentType = "image/Jpeg";  
  19.             context.Response.BinaryWrite(ms.ToArray());  
  20.         }  
  21.     }  
  22.    
  23.     public bool IsReusable {  
  24.         get {  
  25.             return false;  
  26.         }  
  27.     }  
  28. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章