0

I have been asked to try and populate an image control from a sql server DB. When I have done this before I would use the gridview.

Is this possible as I cant find any examples to look at on-line.

I am using a stored Proc and the sqlDataReader.HasRows I want to add the image to the image ID to display on the page.

Before I have used:

<img src='data:image/jpg;base64,<%# Eval("Photo") != System.DBNull.Value ? Convert.ToBase64String((byte[])Eval("Photo")): string.Empty %>'
alt="Person Image" height="80" width="80" />

I am not sure what I would put If the reader.HasRows I was thinking ImageID.Something? then ImageID.DataBind();

* EDIT *

if (rdr.Read())
{
byte[] bytes = (byte[])rdr["Photo"];
string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
img_Pict.ImageUrl = "data:image/jpg;base64," + base64String;
img_Pict.Visible = true;
}
StudentRik
  • 1,049
  • 2
  • 20
  • 37

1 Answers1

0

you can use ASHX generic handler

< img src="image.ashx" >

it must be something like this

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

 public class ImageHandler : IHttpHandler
 {
 public void ProcessRequest(HttpContext context)
  {
    GetFromDb();
  }

private void GetFromDb()
 {
   //..... your database code here ....

  byte[] content = (byte[])db.ExecuteScalar(sql);
  HttpContext.Current.Response.ContentType = "image/jpeg";
  HttpContext.Current.Response.BinaryWrite(content);
}

public bool IsReusable
 {
 get
  {
   return false;
   }
 }

}

Add new ashx to your project and copy the code into it.

When it works, you will be able to get an image as xyzdomain/image.ashx

Rajeev Bera
  • 2,021
  • 1
  • 16
  • 30