Convert from binary data to an image control in ASP.NET

kartal picture kartal · Sep 12, 2011 · Viewed 76.6k times · Source

I have binary data of an image in my database, and I want to display it in an image control in ASP.NET. How? If it is impossible, please find another way to save it in the database and display it in an image control.

Answer

Icarus picture Icarus · Sep 12, 2011

Create a regular HTML img element like so:

<img runat="server" id="image" />

And in code behind do this:

image.src = "data:image/png;base64," + Convert.ToBase64String(imageBytes);

Where imageBytes is a byte[].

You are done. The image will be displayed.