Some Developer
Some Developer

Reputation: 239

How do I convert an image back from its binary data stored in a database?

I have a little problem here.

On my page I use a asp:repeater to read out all the "Projects" from my database. Now each project also contains two images (binary data) and I want to convert them back to images in a handler and use the handler file as the image reference.

I found this code on the web, I also know how it wokrs but I dont know how to use in with a handler:

public Byte[] Ret_image(Int32 id)
{
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "SELECT * FROM Project where Id=@id";
    cmd.Connection = con;
    cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
    SqlDataReader dr = cmd.ExecuteReader();
    dr.Read();
    Byte[] ar = (Byte[])(dr[1]);
    dr.Close();
    cmd.Dispose();
    return ar;
}

I think this would probably work, all I need to do I think is to give the ID of each project, but I don't quite know how to implement this and I don't even know if the code above is correct.

Can anyone help me?

thanks in advance!

[edit]

How do I give the handler the IDs it needs?

<asp:Image ID="img" ImageUrl="Image.ashx" runat="server" Height="80" Width="75%" />

Or how can I get the correct IDs from within the handler?

[EDIT 2]

What am I doing wrong? The Images do not appear, But it doesn't say that it doesn't find them:

<asp:img class="icon-img icon-img_shadow" src="Image.ashx?ID=<%# DataBinder.Eval(Container, "DataItem.id") %>" alt="Icon" width="152" height="140" />

Code in handler:

public void ProcessRequest (HttpContext context) {
    int id = Convert.ToInt32(context.Request.QueryString["ID"]);

    context.Response.ContentType = "image/png";
    MemoryStream strm = new MemoryStream(Ret_image(id));
    byte[] buffer = new byte[4096];
    int byteSeq = strm.Read(buffer, 0, 4096);
    while (byteSeq > 0)
    {
        context.Response.OutputStream.Write(buffer, 0, byteSeq);
        byteSeq = strm.Read(buffer, 0, 4096);
    }
}

public Byte[] Ret_image(Int32 id)
{
    SqlConnection sqlCn = new SqlConnection("Data Source=server;Initial Catalog=db;User ID=user;Password=pw");

    string qry = "SELECT * FROM Project WHERE imageid=@id";
    SqlCommand cmd = new SqlCommand(qry, sqlCn);
    cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
    SqlDataReader dr = cmd.ExecuteReader();
    sqlCn.Open();
    dr.Read();
    Byte[] ar = (Byte[])(dr[1]);
    dr.Close();
    cmd.Dispose();
    sqlCn.Close();
    return ar;
}

Upvotes: 0

Views: 2858

Answers (2)

Fedor Hajdu
Fedor Hajdu

Reputation: 4695

You need to create an ashx page that you'll target as an image URL

Then you need to write the byte array as response stream in the ProcessRequest method inside that handler... Something like:

context.Response.ContentType = "image/jpeg";
strm = new MemoryStream(ar);   // ar here is your variable from Byte[] ar = (Byte[])(dr[1])
byte[] buffer = new byte[4096];
int byteSeq = strm.Read(buffer, 0, 4096);
while (byteSeq > 0)
{
    context.Response.OutputStream.Write(buffer, 0, byteSeq);
    byteSeq = strm.Read(buffer, 0, 4096);
}

Upvotes: 3

Karel Frajt&#225;k
Karel Frajt&#225;k

Reputation: 4489

Just use MemoryStream class and pass the byte array as a constructor parameter:

 MemoryStream ms = new MemoryStream(byteArrayIn);
 Image returnImage = Image.FromStream(ms);
 return returnImage;

Upvotes: 0

Related Questions