Tuesday, February 21, 2012

Retrieve and display image inside an html file (stored in database) in binary format

Hi All,

I am not sure whether this is the right place to post this question. But I am unable to figure out what is the best solution to retrieve and display an image in a html file(stored in varbinary(max) column). I have a list of images in the file and I am supposed to display them. Can anybody please let me know what is the best way to do this?

Thanks a lot!!

HTML image links need to point to a URL. This means you can't just insert the image into the web form. If you have the images stored in a database, you will need to add another web page that will act like the image.

All you'll need in the new webpage is some code to get the image from the database as a byte array. Then write that out that image in the On_PageLoad as follows:

 
Dim img()As Byte' You'll need to add a function that will load the image into the byte array. It will pass in the id passed through the URL. img=LoadImage(Request.QueryString("id"))' Output the image and set the content type to jpeg Response.ContentType ="image/jpeg" Response.Expires = -1 Response.BinaryWrite(img) Response.End()

If the above was in a page called getImage.aspx you could link to it like this

<img src="http://pics.10026.com/?src=getImage.aspx?id=2" />

Hopefully that can get you pointed in the right direction. Let us know if you have any other questions.

No comments:

Post a Comment