Click here to Skip to main content
Rate this: bad
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.
Posted 1-May-11 3:50am
Edited 1-May-11 7:06am
Rate this: bad
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 at 1-May-11 21:25pm
Good one.
Wonde Tadesse at 6-Dec-11 10:31am
Rate this: bad
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:[^]
Big Grin | :-D
Rate this: bad
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 at 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: &lt;asp:Image ID="imghere" runat="server" ImageUrl="mypage.aspx?id=1" /&gt; this only display the first row/image on the database
Rate this: bad
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]
Skrautaray at 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 at 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: bad
Please Sign up or sign in to vote.

Solution 5

To display and save binary formatted image in sql server database in follow this aritcle:
Code of saving and displaying binary image in .net

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 690
1 Dnyaneshwar@Pune 540
2 ravikhoda 325
3 OriginalGriff 279
4 Manas Bhardwaj 230
0 Sergey Alexandrovich Kryukov 10,974
1 OriginalGriff 6,777
2 Peter Leow 4,785
3 Abhinav S 3,813
4 Maciej Los 3,575

Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 7 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid