0

unfortunately I can not display my image using repater and generic handler which I called ImageHandler.ashx, probably I am doing something wrong, and I can not spot mistake by myself.

Here is my code (btw this is first time I am using this generic handler in combination with repeater to get my images from databases and I am stucked here).

Here is my code:

<table class="table table-hover" style="margin-top: 50px; background-color: white;">
<tr>
    <th>Name</th>
    <th>Last Name</th>
    <th>Photo</th>
</tr>
<asp:Repeater ID="repPlayers" runat="server" OnItemCommand="repPlayers_ItemCommand">
    <ItemTemplate>
        <tr>

            <td><%# Eval("Name") %></td>
            <td><%# Eval("LastName") %></td>
            <td>
                <asp:Image ID="imageTest" src="~/ImageHandler.ashx?id=PlayerID" runat="server" />
                <%--<img id="imageTest" style="width: 60px; height: 30px;" src="data:image/png;base64, <%# Convert.ToBase64String((byte[])Eval("Photo")) %> " />--%>
            </td>

        </tr>
    </ItemTemplate>
</asp:Repeater>

ImageHandler.ashx CODE BEHIND:

public class ImageHandler : IHttpHandler
 {
    public void ProcessRequest(HttpContext context)
    {

        int playerID = Convert.ToInt32(context.Request["PlayerID"]);
        Players.Data.DataAccess player = Players.Data.DataAccess.GetByPlayerId(playerID);
        context.Response.ContentType = "image/jpeg";
        context.Response.BinaryWrite(player.Photo);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

Probably I am doing something wrong, maybe I should combine '<%#Eval("PlayerID")%>' somehow or whatever? If I were using datagrid I would use Item_dataBound control and that could solve myproblem but unfortunately I am loading my data here into table and I don't know how to solve this..

edit: I tried this too and that was solution: <asp:Image ID="imageTest" runat="server" ImageUrl='<%#"~/ImageHandler.ashx?PlayerID="+Eval("PlayerID") %>' />

P.S Commented code works but its is loading images slow as hell and that makes my whole page loads very slow because I am getting like 50 objects from database and each of them has image :(

BECAUSE I POST ABOVE WHAT WAS SOLUTION, WE CAN THREAT THIS AS CLOSED!

Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
  • In the commented code you are setting it's mimetype to image/png but in your handler you are setting it to image/jpeg. Are all your images pngs or is there a mix with jpegs (and possibly other formats) as well? – Karl-Johan Sjögren Sep 24 '16 at 20:18
  • for now all of them are .jpg¸and as I said commented code works well with images, but its so slow :/ – Roxy'Pro Sep 24 '16 at 20:19
  • What does it mean "slow"? How do you test it? Against a separate Windows Server or against your dev Windows 7/10? – Wiktor Zychla Sep 24 '16 at 21:34
  • I would like to make it work with ImageHandler.ashx, I believe its better solution. I tested it on windows server / published site (trial hosting) / my local dev machine, whatever, its simply slow with the current solution, that's reason why I commented it and I'm trying to make it work with generic handler.. first solution might be good if we are loading one image, for example profile photo or something, but in general when we are talking about more objects that contains image, looks like that's not good solution. – Roxy'Pro Sep 24 '16 at 21:38

1 Answers1

0

// Here is what I edited to make this works:

<td>
    <asp:Image ID="imageTest" runat="server" style="width: 60px; height: 30px;"
       ImageUrl='<%#"~/ImageHandler.ashx?PlayerID="+Eval("PlayerID") %>' />
</td>
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102