0

I'm trying to list an image on my page from a database. When I eval the image, it just displays the text "System.byte".

Here is the code :

 <asp:Repeater ID="repCategory" runat="server">
     <ItemTemplate>
         <div id="DescofTiles">
             <p id="tiles1"><%#Eval("categoryID")   %>: </p>
             <p id="tiles2"><%#Eval("categoryName") %> </p>
             <br />
             <p id="tiles3"><%#Eval("Description")  %> </p>
             <br />
             <p id="tiles4"><%#Eval("Picture")      %> </p>
             <p id="tiles5">
                 <a id="udb" href="#">
                     <img src="Contents/images/update.png" /></a>
                 <a id="udb" href="DeleteCategory.aspx?id=<%#Eval("categoryID")%>">
                     <img src="Contents/images/delete.jpg" /></a>

                 <asp:LinkButton ID="LinkButton2" runat="server"
                   OnCommand="LinkButton1_Click"
                   CommandArgument='<%#Eval("categoryID")%>'>
                   <img src="Contents/images/delete.jpg" alt="delete group" />
                 </asp:LinkButton>
             </p>

What should I do to make it appear as an image, instead of the text "System.byte"?

M4N
  • 94,805
  • 45
  • 217
  • 260

1 Answers1

0

It seems you have your images stored in a database and you are loading them into a byte[]. (Is that correct? It's not clear from your question).

Here are some options to display these images:

store the image to a (temporary) file

  • write the data from the database to a file on your server, e.g:
  • File.WriteAllBytes(Server.MapPath("~/temp/image.png", byteArrayFromDb));
  • then add an Image control to your page and set the ImageUrl accordingly:
  • <asp:Image ImageUrl="~/temp/image.png ... />`

embed the image data in a data-URI

  • add an Image control to your page, e.g. <asp:Image runat="server" id="img" .../>
  • in your code-behind, convert the byte[] to an image-URI:
  • img.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteArrayFromDb);

serve the images through a HttpHandler

  • implement an HttpHandler which takes a query-string parameter (e.g. the primary-key of the image record)
  • register the handler in your web.config file (e.g. under the name ImageHandler.ashx)
  • use Image URLs such as ImageUrl="ImageHandler.ashx?id=123"
  • see this question for more information
Community
  • 1
  • 1
M4N
  • 94,805
  • 45
  • 217
  • 260
  • no the images are already in the database stored as byte array thanks i will try the second solution – Sari Lakkis Jul 30 '15 at 09:39
  • A 3rd option would be to stream the image through a page... so you'd call `` - and in `streamimage.aspx` you clear all response output, stream the image from the database with a content-type of `image/png` – freefaller Jul 30 '15 at 11:54
  • 2
    @freefaller: or even better, a handler (*.ashx) - I'll add that to the answer – M4N Jul 30 '15 at 11:55