0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 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
  • Possible duplicate of http://stackoverflow.com/questions/19140729/retrieve-image-from-database-and-display-on-asp-net-without-httphandler, http://stackoverflow.com/questions/6456052/display-specific-image-from-sql-database-with-image-control – Stephen Kennedy Oct 21 '14 at 19:39

1 Answers1

5

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").

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48