0

I am using dynamic ItemTemplate for ListView which populated from database.Here is how I do when I need to display string column "Parametr" (or some other datatypes):

     //my .ascx file
     <li><%# Eval("Parametr") %> </li>

How can I display varbinary column that stores images?Thanks.

EDIT:

Here is some more code if somebody needed:

<asp:ListView ...  DataSourceID="database"   ></asp:ListView>
<asp:SqlDataSource ... ID="database" SelectCommand="SELECT image FROM image_table"></asp:SqlDataSource>
Anton Putov
  • 1,951
  • 8
  • 34
  • 62
  • Here is a solution - http://stackoverflow.com/questions/21877/dynamically-rendering-aspimage-from-blob-entry-in-asp-net – Anton Putov Oct 24 '12 at 00:00

1 Answers1

1

You could use an "inline image". The technique is described at for instance Base64 encoded images embedded in html (search for base64 image html for other resources).

Get the base64-encoded string of the image data bytes, using Convert.ToBase64String for instance, and then use <img src=data:image/gif;base64, and append the image data.

So you can bind it using something like this

<img src='<%# string.Format("data:image/gif;base64,{0}",
 Convert.ToBase64String((byte[])Eval("ImageDataBase64")))%>'/>

Of course, this is only recommended for small images. Also note that you should change "gif" to the actual format of your image.

Patrick
  • 17,669
  • 6
  • 70
  • 85
  • @AntonPutov: That's unfortunate. Unfortunately I can't help you, since you won't tell me what the problem is. – Patrick Oct 21 '12 at 22:50
  • my problem is simple - here is template [
  • some text
  • ] - blob - binary image data (gif).its not work).Are there another ways to solve this problem? – Anton Putov Oct 21 '12 at 22:55
  • What? The problem could be "InvalidOperationException", or "SqlException". I can't believe the error you are seeing on your web page is "It doesn't work"? – Patrick Oct 21 '12 at 22:59
  • What is "blob" in your example? – Patrick Oct 21 '12 at 23:00
  • @AntonPutov: What is blob then? If you inspect the code, do you have any text after the `,` in the image source? – Patrick Oct 22 '12 at 18:19
  • I can't help you if all you keep saying is "it doesn't work", you have to specify the problem you are facing. *Why does it not work*? – Patrick Oct 22 '12 at 18:20
  • I edit my question - blob is varbinary column which saves image – Anton Putov Oct 22 '12 at 18:21
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/18421/discussion-between-anton-putov-and-patrick) – Anton Putov Oct 22 '12 at 18:30
  • Again, you have to convert it to a base64 string first, you can't use a byte array as the argument to the string format method, because it won't work, as described in the answer – Patrick Oct 22 '12 at 18:39