2

Possible Duplicate:
How to deserialize json image(byet array)into image in asp.net?

The byte array gets converted into the integer representation of each byte. When viewed in Fiddler it look likes

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

How to retrieve this integer image array from json web service as class in asp.net c#

Community
  • 1
  • 1
Narasi
  • 75
  • 1
  • 2
  • 13

1 Answers1

1

Assuming you have a byte array:

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

you could create an Image:

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

or if you wanted to display it on an ASP.NET web form, inside an Image control, you could write a generic handler that will stream this image to the response:

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";
    }
}

and then point the Image control to this generic handler:

<asp:Image runat="server" ID="myimage" ImageUrl="~/imagehandler.ashx" />
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928