Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display a binary stream fetched from database as "image" inside the html as an <img> tag

I am trying to display an image which is fetched from the database as a binary stream, on the web form. Can you shed some light on this!

I am working on a project where users have profiles, and every user have their own profile picture displayed in the home page. THis picture comes from the database.

here is the code i have used to get the stream !

TestDBDataContext context1 = new TestDBDataContext();

            var r = (from a in context1.ImageTables where a.Id == 8 select a).First();

           MemoryStream stream = new MemoryStream(r.FileImage.ToArray());

thanks

like image 384
Muaado Avatar asked Dec 16 '11 15:12

Muaado


2 Answers

Base64 encode your binary and insert in an image tag as follows (change the mimetype to suit):

<img src="data:image/gif;base64,BASE64 ENCODED BINARY HERE">

This is how you would do it, but I would not do this as some browsers cannot handle it (IE6). You are better off saving to a file first and doing it the conventional way.

like image 187
Adam Pointer Avatar answered Oct 02 '22 02:10

Adam Pointer


What I've done in the past is set the image url to an aspx page, like so:

<img src="getLogo.aspx" alt="Logo" />

Then, in the code-behind for getLogo.aspx, I've done the following:

Response.BinaryWrite( imageData );
Response.ContentType = "image/gif";

Where imageData is your image as a byte array

like image 28
DJ Quimby Avatar answered Oct 02 '22 02:10

DJ Quimby