• 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属性定位服务器端输出图片流的页面

  • 相关阅读:
    2019 年 GrapeCity Documents 产品路线图
    2019 年 Spread.NET 产品路线图(Roadmap)
    ActiveReports报表控件 V13 正式发布,提供在线报表设计和自适应报表布局
    ActiveReports 大数据分析报告:2018中国电影再次迎来黄金时代
    【Visual Studio 扩展工具】如何在ComponentOne的DataTree中实现RightToLeft布局
    JavaScript-页面打印正方形,各种三角形与菱形
    HTML入门与基础 —— 标签《一》
    springmvc异常处理
    springmvc-数据回显
    springmvc-validation校验
  • 原文地址:https://www.cnblogs.com/andy65007/p/1643822.html
Copyright © 2020-2023  润新知