zoukankan      html  css  js  c++  java
  • ASP.NET直接输出图片文件到页面

    代码
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>
    <%@ import namespace="System.IO"  %>
    <%@ import namespace="System.Drawing"  %>
    <%@ import namespace="System.Drawing.Imaging"  %>

    <!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>
        
    <script type="text/C#" runat="server">
        protected 
    void Button1_Click(object sender, EventArgs e)
        {
            
    if (Request.Files.Count>0)
            {
                
    //Get file in byte format
                HttpPostedFile f = Request.Files[0];
                
    byte[] buf = new byte[f.ContentLength];
                f.InputStream.Read(buf, 
    0, buf.Length);

                MemoryStream ms 
    = new MemoryStream(buf);
                Bitmap map 
    = new Bitmap(ms);
                Response.Clear();
                map.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                Response.End();

                map.Dispose();
                ms.Close();
            }
        }
        
    </script>
    </head>
    <body>
        
    <form id="form1" runat="server">
        
    <div>
            
    <asp:fileupload id="FileUpload1" runat="server" />
            
    <asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" />
        
    </div>
        
    </form>
    </body>
    </html>

    IE客户端得到的源代码

    IE客户端得到的结果
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
        
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body>
        
    <img src="http://localhost:25715/WebSite7/Default5.aspx"></body></html>

    注意,客户端使用img的src属性定位服务器端输出图片流的页面

  • 相关阅读:
    菜鸟二三事
    访问 IIS 元数据库失败的问题(转)
    SQL Server 2005/2008还原数据库时遇到的问题(转)
    ME54N审批、撤批触发增强点:ME_RE…
    南通网站建设整理:最新搜索引擎登录口保证都可以用
    调试mvc的源代码
    c#委托(delegate)揭秘
    ASP.NET 应用程序生命周期概述
    JavaScript Array(数组)对象
    jQuery.each
  • 原文地址:https://www.cnblogs.com/andy65007/p/1643822.html
Copyright © 2011-2022 走看看