Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 746
Farshid Avatar asked Nov 20 '25 03:11

Farshid


2 Answers

You could put <img src="data:image/png;base64,<BASE64 STRING>" />, so you would set the asp:image's ImageUrl property to "data:image/png;base64,xxx".

However, I suspect the browser support on this is spotty, works fine in IE9 and firefox, but I'm unsure of older browsers support for this.

An alternative I would recommend though, is to create a generic handler ashx, that reads the database and returns an image. You can check out this website on how to do it: http://www.dotnetperls.com/ashx, you would then set the ImageUrl property to this handlers address.

like image 136
Matthew Avatar answered Nov 21 '25 16:11

Matthew


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);
}
like image 25
rick schott Avatar answered Nov 21 '25 18:11

rick schott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!