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
759k185 gold badges1.4k silver badges1.5k bronze badges
asked Oct 21, 2014 at 19:15
6
  • 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 Commented Oct 21, 2014 at 19:17
  • You should be wrapping your reader, your MemoryStream, your cmd and your connection in using blocks. Commented Oct 21, 2014 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. Commented Oct 21, 2014 at 19:27
  • 1
    ...or write an ashx handler to fetch the image from the db and return it as a binary stream. Commented Oct 21, 2014 at 19:28
  • 1
    I'm pretty sure that saving a path of the picture is not a good idea, but okay :) Commented Oct 21, 2014 at 19:32

1 Answer 1

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

answered Oct 21, 2014 at 19:32

2 Comments

Of course, this just about doubles the download size of the image.
This solution reveals a horrible design. This won't scale.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.