Click here to Skip to main content
14,603,943 members
Rate this:
Please Sign up or sign in to vote.

I want to show a binary format image in my .aspx page.

I am using,C# and ms sql 2005 database.

I can store am image into database image format filed. but i cant not show it in image.

I can retrieve the image format value in a variable but cant show it in may image.

this is my code...
 byte[] img = (byte[])dt.Rows[0]["logo"];
Image1.ImageUrl = "~/" + img + "";

can any one help me??

thanks in advance.

Updated 1-May-11 7:06am
Rate this:
Please Sign up or sign in to vote.

Solution 3

You can not simply bind binary image to a control. You have to create an HttpHandler(.ashx) that process the image.

using System;
using System.Web;

public class ImageHandler : IHttpHandler, IReadOnlySessionState

    public void ProcessRequest(HttpContext context)
        context.Response.BinaryWrite(yourImageByte);// You must replace "yourImageByte" with image reader logic from database.

    public bool IsReusable {
        get { return false; }


Finally you can bind the image to the handler.
Image1.ImageUrl ="ImageHanderl.ashx"

For more information look this[^] article.
Prasanta_Prince 1-May-11 21:25pm
Good one.
Wonde Tadesse 6-Dec-11 10:31am
muneeruc 18-Dec-14 6:42am
absolutely right Answer
Wonde Tadesse 18-Dec-14 7:34am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Do you store the image data in your database, or the image file name?
The code above looks like you are storing teh file name, but teh description reads as if it is the actual image data.

If it is the file name, that's easy: convert your byte[] to a string:
byte[] img = (byte[])dt.Rows[0]["logo"];
string s = System.Text.Encoding.ASCII.GetString(img);
Image1.ImageUrl = "~/" + s;
If it is the content, then it is slightly more complex.

[edit]Forgot to change "img" to "s" in final line - OriginalGriff[/edit]
Santosh Rautaray 1-Sep-12 13:05pm
i wants to display binary image without using (Hander.ashx) control.
Suppose i have 100 binray images in a datatable and i wants to display all at a time without going to database no. of times.
OriginalGriff 2-Sep-12 2:19am
Open a new question, and provide full details there.
(It's not polite to tack a different problem onto an existing question - plus it's difficult to answer without confusing the OP)
Rate this:
Please Sign up or sign in to vote.

Solution 2

You have to create a MemoryStream which will take the Byte array as argument, and then create an Image using that.

MemoryStream ms = new MemoryStream(img);
Image1 = Image.FromStream(ms);

Credit goes to this:[^]
Rate this:
Please Sign up or sign in to vote.

Solution 4

Another alternative:
Make a page with only code:

protected void Page_Load(object sender, EventArgs e)

            Response.ContentType = "image/jpeg";
            //Setup connection or whatever you like and read jpg file
            //assuming it is jpg of cour or else change above to
            // "image/_relatedformat_"
            // check "" for more info
            byte[] img = (byte[])dt.Rows[0]["logo"];
            Response.BinaryWrite( img );

and call this page from wherever you like as:
<Img src="" ></img>
Matt Jun 9-Sep-11 1:50am
what if I want to display an image depending on its database ID?

I have here the code:

void Show_Logo(object data)
if (data != null)
if (Request.QueryString["id"] != null)
Byte[] bytes = ((ihaveimage)data).Logo;
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = ((ihaveimage)data).Logo.ToString();


<asp:Image ID="imghere" runat="server" ImageUrl="mypage.aspx?id=1" />

this only display the first row/image on the database

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100