采用读取文件流的方式,显示远程网站上的图片,用于解决远程网站为HTTPS地址且证书无效的情况。
图片流获取:
1 <%@ WebHandler Language="C#" Class="ImageView" %> 2 3 using System.Drawing; 4 using System.IO; 5 using System.Net; 6 using System.Web; 7 8 public class ImageView : IHttpHandler 9 { 10 public void ProcessRequest(HttpContext context) 11 { 12 string url = context.Server.UrlDecode(context.Request.QueryString["url"]); 13 if (!string.IsNullOrEmpty(url)) 14 { 15 WebRequest request = WebRequest.Create(url); 16 Stream stream = request.GetResponse().GetResponseStream(); 17 if (stream != null) 18 { 19 var image = new Bitmap(stream); 20 var ms = new MemoryStream(); 21 image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); 22 23 context.Response.ContentType = "image/jpeg"; 24 context.Response.BinaryWrite(ms.ToArray()); 25 context.Response.End(); 26 } 27 } 28 } 29 30 public bool IsReusable 31 { 32 get 33 { 34 return false; 35 } 36 } 37 }
图片显示:
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <title>显示远程网站上的图片</title> 5 <script> 6 window.onload = function () { 7 var imageUrl = encodeURI("http://www.baidu.com/img/baidu_jgylogo3.gif"); 8 document.getElementById("image").src = "ImageView.ashx?url=" + imageUrl; 9 }; 10 </script> 11 </head> 12 <body> 13 <img id="image" alt="远程图片" src="#" /> 14 </body> 15 </html>