【问题标题】:Display image from byte array从字节数组显示图像
【发布时间】:2013-12-18 06:42:24
【问题描述】:

我在会话中存储了图像字节数组。

    Byte[] bytes = (Byte[])Session["STORED_IMAGE"];

我想在回发后在图像控件中显示它。我试过这段代码

    Byte[] bytes = (Byte[])Session["STORED_IMAGE"];
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "image/jpeg";
    Response.AddHeader("content-disposition", "attachment;filename=sandra");
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();

它显示图像。但也下载它。我只想显示不下载。 任何人都可以帮我做这件事吗?在此先感谢。

【问题讨论】:

    标签: c# asp.net image


    【解决方案1】:

    删除那一行

     Response.AddHeader("content-disposition", "attachment;filename=sandra");
    

    这一行给出“命令”,浏览器开始下载它。

    更新

    如果我理解得很好,您尝试在页面的某个位置显示此图像。 但我也了解您将该代码与其余代码添加在同一页面上。这不起作用,因为您“破坏”了页面。

    创建一个处理程序 .ashx 并在那里放置该代码。

    然后从您的页面中调用该处理程序

    <img src="showimage.ashx" />

    你看到了。

    【讨论】:

    • 我已经尝试过了。然后其他内容消失,只出现图像
    • @Sandra 如果您在回发时添加了此代码并覆盖了页面流,那么您还有其他问题...制作一个处理程序并显示从该处理程序调用的图像
    • 我试过用这个处理程序 public void ProcessRequest(HttpContext context) { var imageData = context.Session["STORED_IMAGE"] as byte[]; context.Response.OutputStream.Write(imageData, 0, imageData.Length); context.Response.ContentType = "图像/JPEG"; } public bool IsReusable { get { return false; } }
    • 如果我在Handler中添加,图片不显示
    • @Sandra 你看到你遇到了什么错误吗?可能您没有在处理程序上添加会话。我这么说是因为你使用会话。打开处理程序的结果或调试它以查看您的错误...阅读此内容:stackoverflow.com/questions/14181408/…
    【解决方案2】:

    添加处理程序

    带参数

     <asp:Image runat="server" Width="40px" Height="40px" ImageUrl='<%# "Handler.ashx?VehicleCode=" + Eval("VehicleCode")%>'>
    

    无参数

     <asp:Image runat="server" Width="40px" Height="40px" ImageUrl='<%# "Handler.ashx %>'>
    

    内部处理程序从 db 获取图像并作为 this 传递

     public void ProcessRequest(HttpContext context)
        {
    
          //if you pass parameter use this
            string para = context.Request.QueryString["VehicleCode"];
    
          //get the image from data base in here im using a web service
            System.Data.DataSet ds = new System.Data.DataSet();
            MMS_MasterWebService.MMS_MasterMaintenance obj = new MMS_MasterWebService.MMS_MasterMaintenance();
            obj.Url =  "http://192.168.48.10/SHOREVision_MMS_Service/MMS_MasterMaintenance.asmx";
            ds = obj.GetVehicleMasterByCode(para);
            context.Response.BinaryWrite((byte[])ds.Tables[0].Rows[0][21]);
    
        }
    

    【讨论】:

    • @sandra 使用此链接查看如何创建处理程序并在网页上显示图像dotnetperls.com/ashx
    猜你喜欢
    • 2017-01-13
    • 2013-08-07
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 2019-09-21
    • 1970-01-01
    • 2012-03-25
    • 2013-07-24
    相关资源
    最近更新 更多