Tharik Kanaka
Tharik Kanaka

Reputation: 2510

How to Display byte image on the asp.net page dynamically?

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

Upvotes: 4

Views: 3096

Answers (1)

Jay Otterbein
Jay Otterbein

Reputation: 968

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.

Upvotes: 4

Related Questions