3

How can I use Eval for binding sql varbinary data (images) to image? Something like this:

   <image src = <%# Eval("imageBinaryData") %> />

1 Answers1

1

You need to use a HttpHandler to fetch the data and stream it back. You would then link to the handler from your ASPX page.

<img class="mainEventsImage" 
    src='<%# Eval("MainImagePath").ToString().Replace("\\", "/") %>' 
        alt='<%# Eval("Title") %>' runat="server" />

if (reader.Read())
{
    int bufferSize = 100;
    byte[] bytes = new byte[bufferSize];
    long bytesRead;
    long readFrom = 0;

    do
    {
        bytesRead = reader.GetBytes(0, readFrom, bytes, 0, bufferSize);
        context.Response.ContentType = "image/jpeg";
        context.Response.BinaryWrite(bytes);
        readFrom += bufferSize;
    }
    while (bytesRead == bufferSize);
}
reader.Close();
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • i want to do it like this article: http://stackoverflow.com/questions/13002578/bind-database-image-to-itemtemplate-in-ascx/13002874#comment17666856_13002874 –  Oct 22 '12 at 19:55
  • It won't work without some major workarounds. Example in answer is the normal way to accomplish this. Similar questions: http://stackoverflow.com/search?q=asp.net+image+handler – IrishChieftain Oct 22 '12 at 19:57
  • your link save me a lot of time.thanks a lot.Here is I like more:http://stackoverflow.com/questions/21877/dynamically-rendering-aspimage-from-blob-entry-in-asp-net –  Oct 22 '12 at 23:15