博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET读取网络图片并在页面上显示
阅读量:7022 次
发布时间:2019-06-28

本文共 1777 字,大约阅读时间需要 5 分钟。

/Ferry/archive/2011/01/18/1937974.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".hk/intl/zh-CN/images/logo_cn.png"

            alt="google logo" />
    
</div>
    
</form>
</body>
</html>

Handler.ashx

<%
@ WebHandler Language
=
"
C#
"
 Class
=
"
Handler
"
 
%>
using
 System;
using
 System.Web;
using
 System.Net;
using
 System.Drawing;
using
 System.IO;
public
 
class
 Handler : IHttpHandler {
    
    
public
 
void
 ProcessRequest (HttpContext context) {
        
string
 imgUrl 
=
 context.Request[
"
Url
"
];
        
if
 (
!
string
.IsNullOrEmpty(imgUrl))
        {
            Uri myUri 
=
 
new
 Uri(imgUrl);
            WebRequest webRequest 
=
 WebRequest.Create(myUri);
            WebResponse webResponse 
=
 webRequest.GetResponse();
            Bitmap myImage 
=
 
new
 Bitmap(webResponse.GetResponseStream());
            MemoryStream ms 
=
 
new
 MemoryStream();
            myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            context.Response.ClearContent();
            context.Response.ContentType 
=
 
"
image/Jpeg
"
;
            context.Response.BinaryWrite(ms.ToArray());
        }
    }
 
    
public
 
bool
 IsReusable {
        
get
 {
            
return
 
false
;
        }
    }
}

补充:

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

<%
@ WebHandler Language
=
"
C#
"
 Class
=
"
Handler2
"
 
%>
using
 System;
using
 System.Web;
using
 System.IO;
using
 System.Drawing;
public
 
class
 Handler2 : IHttpHandler {
    
public
 
void
 ProcessRequest(HttpContext context)
    {
        
string
 path 
=
 context.Request.QueryString[
"
path
"
];
        
if
 (
!
string
.IsNullOrEmpty(path))
        {
            FileStream fs 
=
 
new
 FileStream(@path, FileMode.Open, FileAccess.Read);
            Bitmap myImage 
=
 
new
 Bitmap(fs);
            
            MemoryStream ms 
=
 
new
 MemoryStream();
            myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            context.Response.ClearContent();
            context.Response.ContentType 
=
 
"
image/Jpeg
"
;
            context.Response.BinaryWrite(ms.ToArray());
        }
    }
 
    
public
 
bool
 IsReusable {
        
get
 {
            
return
 
false
;
        }
    }
}

转载于:https://www.cnblogs.com/webdesign/archive/2011/01/25/tt118.html

你可能感兴趣的文章
Codeforces Round #294 (Div. 2) A and B and Lecture Rooms(LCA 倍增)
查看>>
SpringBoot 使用MultipartFile上传文件相关问题解决方案
查看>>
selenium-如何上传非input格式的图片
查看>>
C - Visible Trees HDU - 2841 -莫比乌斯函数-容斥
查看>>
leetcode-121-Best Time to Buy and Sell Stock
查看>>
leetcode-166-分数到小数(用余数判断有没有出现小数的循环体)
查看>>
初探nginx
查看>>
UVa 11369 - Shopaholic
查看>>
不会玩魔兽的项目经理不是好项目经理
查看>>
数据库范式
查看>>
〖Linux〗使用gsoap搭建web server(C++)
查看>>
深入理解C++的动态绑定和静态绑定
查看>>
【PHP】 PHPqrCode二维码类库使用方法
查看>>
最新亲测能用的手机号码正则
查看>>
如何模拟世界 -- 一个奇怪的想法
查看>>
BI笔记之---合理处理SSAS数据库的几点建议
查看>>
Java Web学习总结(8)——使用Cookie进行会话管理
查看>>
python安装
查看>>
Oracle EBS-SQL (BOM-9):检查系统BOM总数.sql
查看>>
PhpStorm 常用快捷键
查看>>