c# - 二进制数组在asp.net中成像

标签 c# asp.net json web-services

<分区>

Possible Duplicate:
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 网络服务 中检索此整数图像数组 作为 asp.net c# 中的类

最佳答案

假设你有一个字节数组:

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 网络表单上显示它,请在 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" />

关于c# - 二进制数组在asp.net中成像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14117731/

相关文章:

javascript - 为什么我的 jQuery 代码无法读取撇号?

php - MySQL 数据处理。如何从数据库获取斜杠后面的值

c# - 如何通过局部变量获取线程结果?

c# - C#中异步调用的设计模式

asp.net - 如何将 Visual Studio "compile"制作为 .aspx 文件?

asp.net - 部署在服务器上时出现奇怪的 IE6 和 7 错误

c# - 如何使用 C# 以编程方式删除 IE 代理

c# - 您要在 C# 中获取 Windows 安装 SID 吗?

asp.net - 如何更新 aspnetdb 成员资格 IsApproved 值?

android - 使用 JSONArray 或普通数组来存储/读取数据是否更有效?