Farshid Farshid - 1 year ago 150
ASP.NET (C#) Question

Display image from a datatable in asp:image in code-behind

I have a datatable which is filled from the resultset of a 1 row select statement (via a stored procedure in SQL Server 2008) and it contains a

typed column which I store images in.

I have an
control on an aspx page and i want to set the image to the corresponding field of that datatable but anything I do I can not.
Please tell me how can I set the
to image column of that datatable from the code behind.

Answer Source

Try the Data URL scheme:

<img src="<%# ReturnEncodedBase64UTF8(Eval("ColumnA")) %>" />

protected static string ReturnEncodedBase64UTF8(object rawImg)
    string img = "data:image/gif;base64,{0}"; //change image type if need be
    byte[] toEncodeAsBytes = (byte[])rawImg;        
    string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
    return String.Format(img, returnValue);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download