Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,

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

I am using asp.net,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...
C#
 byte[] img = (byte[])dt.Rows[0]["logo"];
Image1.ImageUrl = "~/" + img + "";


can any one help me??

thanks in advance.

Rashed
Posted
Updated 1-May-11 7:06am
v3

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

C#
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.
C++
Image1.ImageUrl ="ImageHanderl.ashx"

For more information look this[^] article.
 
Share this answer
 
Comments
Prasanta_Prince 1-May-11 21:25pm    
Good one.
Wonde Tadesse 6-Dec-11 10:31am    
Thanks
muneeruc 18-Dec-14 6:42am    
absolutely right Answer
Wonde Tadesse 18-Dec-14 7:34am    
Thanks
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]
 
Share this answer
 
v2
Comments
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)
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:

http://www.codeproject.com/Members/d-nish?msg=3367481#xx3367481xx[^]
:D
 
Share this answer
 
Another alternative:
Make a page with only code:

C#
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 "http://msdn.microsoft.com/en-us/library/ms775147.aspx" for more info
            byte[] img = (byte[])dt.Rows[0]["logo"];
            Response.BinaryWrite( img );
        }


and call this page from wherever you like as:
<Img src="http://yourdomain.com/yourpage.aspx" ></img>
 
Share this answer
 
Comments
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.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = ((ihaveimage)data).Logo.ToString();
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
}
}

THEN:

<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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900