4

I have loaded an byte array image from a web service to my asp.net website. I need to display it on the web page as soon as execute the web service.

I have tried using generic handler, but i could not do it since there wasn't way to pass byte[] image into the generic handler

void Button2_Click1(object sender, EventArgs e)
{

    this.verifyTemplates();
    byte[] userImg;
    try
    {

        matchTemp.templateService hs = new matchTemp.templateService();
        bool s1 = hs.matchTemplates(template, out userID, out userName, out userImg);
// userImg is the byte image i need to display

    }
    catch(Exception exc)
    {
     //   vLabel.Text = exc.Message;
    }
}
4
  • 1
    Where is the image data coming from? Where is the "generic handler" code? What exactly are you trying to do in the click event handler?
    – Oded
    Mar 23, 2012 at 11:41
  • 1
    Please read - Writing the perfect question.
    – Oded
    Mar 23, 2012 at 11:42
  • Forget about the generic handler part button2 is an asp button, at the click event i have executed a web service called matchTemplates and it gives few out parameters and last out parameter is a byte [] image. i need to display that image on the page Mar 23, 2012 at 11:45
  • There are no controls that allow that. You need to use an image control and point its source to a HttpHandler that returns the dynamic image. There are many questions and answers on SO describing how to achieve that.
    – Oded
    Mar 23, 2012 at 11:47

1 Answer 1

4

What you're looking for is a Data URL. You can get the base64 of a byte array like this (change the image type as required)

string imageBase64 = Convert.ToBase64String(userImg);
string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);

In the view:

<img src='"<%=imageSrc%>"' />

This won't work in IE 7 or earlier, if you need to support those then you'll want to look at MHTML.

1
  • 1
    Seeing as converting to Base64 adds about 30% to the size of the encoded value, this may not be an option.
    – Oded
    Mar 23, 2012 at 11:48

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.