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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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);
}
Method 3
Display Images from SQL Server Database using ASP.Net
aspx file
<asp:image ID="Image1" runat="server" ImageUrl ="ImageCSharp.aspx?ImageID=1"/>
cs file
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["ImageID"] != null)
{
string strQuery = "select Name, ContentType, Data from tblFiles where <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="80e9e4bdc0e9e4">[email protected]</a>";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("@id", SqlDbType.Int).Value
= Convert.ToInt32 (Request.QueryString["ImageID"]);
DataTable dt = GetData(cmd);
if (dt != null)
{
Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = dt.Rows[0]["ContentType"].ToString();
Response.AddHeader("content-disposition", "attachment;filename="
+ dt.Rows[0]["Name"].ToString());
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
}
Method 4
Simply by using Convert.ToBase64String
byte[] bytes = (byte[])dr["YourImageField"];
string b64img = Convert.ToBase64String(bytes);
Image1.ImageUrl = "data:image/jpeg;base64," + b64img;
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0