Response输出图像

来源:互联网 发布:udacity网络爬虫 编辑:程序博客网 时间:2024/06/03 02:09

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace ="System.IO" %>
<script runat = "server">

    protected void Page_Load(object sender, EventArgs e)
    {
        //指定被输出图像的地址
        string strPath = Request.MapPath("~/Images/ajax.gif");
       
        //创建文件流,读取将要输出的图像
        FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);
       
        //定义保存图像数据的二进制数组
        byte[] imageData = new byte[(int)fs.Length];
   
        //读取二进制数组
        fs.Read(imageData, 0, (int)fs.Length);
   
        //输出二进制数组(图像)
        Response.BinaryWrite(imageData);

        //设置页面的输出格式. 具体格式见 Web文件的ContentType类型大全
        Response.ContentType = "image/gif";

        //中止页面的其他输出
        Response.End();
    }   
   
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>图像输出</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

 

运行结果:

 

 

原创粉丝点击