DNR
DNR

Reputation: 3746

Displaying an image from SQL Server in asp.net

I am need to display an image on my asp.net page, which is stored as an image data type in SQL Server. I am looking at this post, display/retrieve image from sql database in vb.net, however, how can I display that image in a <td> element?

Upvotes: 0

Views: 4356

Answers (2)

Icarus
Icarus

Reputation: 63970

<td> tags can't be used to display images unless you put an <img> element inside it. With that said, this is how you display an image in VB.NET from code behind:

Having this type of markup in your aspx page:

<td> 
<img src="" runat="server" id="imageFromDB" />
... 

You can do this in code behind:

Dim imageBytes() as Byte= ...  // You got the image from the db here...
//jpg is used as an example on the line below. 
//You need to use the actual type i.e. gif, jpg, png, etc.
//You can do imageFromDB simply because you set runat="server" on the markup
imageFromDB.src = String.Format("data:image/{0};base64,
{1}","jpg",Convert.ToBase64String(imageBytes)

And this will render your image on the page.

I hope I've given you enough information here.

Upvotes: 3

Jan
Jan

Reputation: 16048

Referring to the sample in the answer you mention:

<td><img src="PathToYourHttpHandler?id=ID_OF_YOUR_IMAGE" /></td>

Upvotes: 1

Related Questions