【问题标题】:Binary Array to image in asp.net [duplicate]二进制数组到asp.net中的图像[重复]
【发布时间】:2013-01-02 06:24:38
【问题描述】:

可能重复:
How to deserialize json image(byet array)into image in asp.net?

字节数组被转换为每个字节的整数表示。在 Fiddler 中查看时,它看起来像

 {"imageBackground":[137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,...]}

如何从 json web 服务 中检索这个 整数图像数组 作为 asp.net c# 中的类

【问题讨论】:

    标签: c# asp.net json web-services


    【解决方案1】:

    假设你有一个字节数组:

    byte[] imageBackground = new byte[] { 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,... };
    

    你可以创建一个Image:

    using (var stream = new MemoryStream(imageBackground))
    using (var image = Image.FromStream(stream))
    {
        // do something with the image
    }
    

    或者,如果您想在 ASP.NET Web 表单上显示它,在 Image 控件内,您可以编写一个通用处理程序,将这个图像流式传输到响应:

    public class ImageHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }
    
        public void ProcessRequest(HttpContext context)
        {
            var response = context.Response;
            byte[] imageBackground = new byte[] { 137,80,78,71,13,10,26,10,0,0,0,13,73,72,68,... };
            response.OutputStream.Write(imageBackground, 0, imageBackground.Length);
            response.ContentType = "image/jpeg";
        }
    }
    

    然后将 Image 控件指向这个通用处理程序:

    <asp:Image runat="server" ID="myimage" ImageUrl="~/imagehandler.ashx" />
    

    【讨论】:

      猜你喜欢
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 2018-06-30
      相关资源
      最近更新 更多