Question

I need to save a Image i create from a byte[] into a Image control in ASPX page. So far:

byte[] myByte = (byte[])row["image"];
MemoryStream myStream = new MemoryStream(myByte, false);
System.Drawing.Image sampleImage = System.Drawing.Image.FromStream(myStream);

Then I need to set this image into a System.Web.UI.WebControls.Image

How can I do this in the same aspx page, without any external .ashx or .asmx file ? Basically what i need is to set a byte[] coming from db into a Image Control

Was it helpful?

Solution

Here is how to setup embedded base64 encoded image (provided the image is png):

<asp:Image runat="server" ID="foo" />

foo.Attributes["src"] = "data:image/png;base64," + 
    Convert.ToBase64String(myByte);

Again - this should be avoided; you can't cache images served like this; so if you really really want to serve images from db you should consider external handler/page to serve them (and set up caching on that handler).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top