Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I want to retrieve an image from a SQL Server database and show in a Image tool.

I have this code

<asp:Image ID="Image1" runat="server" CssClass="style2" Height="166px" Width="488px" />


SqlConnection connect = null;

string connectstring = "Data Source=.\\SQLEXPRESS;Initial Catalog=teste;Integrated Security=true;pooling=false";
connect = new SqlConnection(connectstring);
connect.Open();

string Scmd = "SELECT id, imagem where id = 2";
SqlCommand cmd = new SqlCommand(Scmd, connect);

SqlDataReader reader = cmd.ExecuteReader();

reader.Read();

if (reader.HasRows)
{
    Label1.Text = reader[0].ToString();                        
    byte[] imagem = (byte[])(reader[1]);

    MemoryStream ms = new MemoryStream(imagem);

    Image1.ImageUrl = ms.FromStream(ms); //i tried this
}

But I can't do this:

Image1.ImageUrl = ms.FromStream(ms);

because I get an error.

Somebody please can help me? The only problem I have is show the image.

Please help, thanks.

share|improve this question
2  
the recommended way is to store the path to your image, so that once retrieved from the db, you can build the url to the image – meda Oct 21 '14 at 19:17
    
You should be wrapping your reader, your MemoryStream, your cmd and your connection in using blocks. – Icemanind Oct 21 '14 at 19:22
    
There is no way to answer this question since you don't show how Image1 is declared. Is this a web application? If so then you will need a url. You will also probably need a server to deliver the data from the database when that URL is accessed. OR you could store the image as a file and use a standard url no service needed. – Hogan Oct 21 '14 at 19:27
1  
...or write an ashx handler to fetch the image from the db and return it as a binary stream. – Stephen Kennedy Oct 21 '14 at 19:28
1  
I'm pretty sure that saving a path of the picture is not a good idea, but okay :) – mybirthname Oct 21 '14 at 19:32

1 Answer 1

up vote 2 down vote accepted

You can generate base64string out of byte array and use that as inline image source.

  1. Convert to base64string. Refer this.
byte[] imagem = (byte[])(reader[1]);
string base64String = Convert.ToBase64String(imagem) ;
  1. Use this string as inline image like following. Refer this.

Image1.ImageUrl = String.Format("data:image/jpg;base64,{0}",base64String);

Assumption: image saved as jpg.

If this differs, then change line in step#2.

Disclaimer: Base64string as image is suitable for small sized image , but if we have bigger image, then the string produced will have large sized string. Advantage is , browser don't have to request multiple times for each image ( if you implemented this as image.src= "http://handler-to-image").

share|improve this answer
1  
Tanks very mutch for the help. 100% work – you are been banned Oct 21 '14 at 19:47
    
Of course, this just about doubles the download size of the image. – rossisdead Oct 21 '14 at 20:38
1  
This solution reveals a horrible design. This won't scale. – Hogan Oct 22 '14 at 13:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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