ASP.NET读取网络或本地图片显示

来源:互联网 发布:qq通讯录群发软件 编辑:程序博客网 时间:2024/05/10 20:07

写这个的缘由是在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. }  

原创粉丝点击