Farshid Farshid - 3 months ago 16
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

Image
typed column which I store images in.

I have an
asp:image
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
asp:image
to image column of that datatable from the code behind.

Answer

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);
}