C# .NET - Bind Image to gridview at runtime

Asked By sangeetha simma on 24-Apr-12 05:00 AM
hi...
I need to bind Binary data(image) from sqldatabase directly to gridview.So i used gridview rowdatabound and wrote some code..that code is working but how can i bind that image to gridview at runtime..plz verify below code and give me any solution..


public void gvStudent_RowDataBound(object sender, GridViewRowEventArgs e)
    {
  if (e.Row.RowType == DataControlRowType.DataRow)
      {
for (int j = 0; j < ds2.Tables[0].Rows.Count; j++)
        {
  byte[] objbyteArray = (byte[])(ds2.Tables[0].Rows[j]["image"]);
                MemoryStream ms = new MemoryStream(objbyteArray);
                System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
}
   } 
 }
dipa ahuja replied to sangeetha simma on 24-Apr-12 05:04 AM
Step 1: Add a handle in your website and write this code in ProcessRequest:
public void ProcessRequest (HttpContext context)
{
  SqlConnection con = new SqlConnection();
  con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 
  // Create SQL Command
 
  SqlCommand cmd = new SqlCommand();
  cmd.CommandText = "Select pname,images from Photos where id =@id";
  cmd.CommandType = System.Data.CommandType.Text;
  cmd.Connection = con;
 
  SqlParameter ImageID = new SqlParameter("@id", System.Data.SqlDbType.Int);
  ImageID.Value = context.Request.QueryString["id"];
  cmd.Parameters.Add(ImageID);
  con.Open();
  SqlDataReader dReader = cmd.ExecuteReader();
  dReader.Read();
  context.Response.BinaryWrite((byte[])dReader["images"]);
  dReader.Close();
  con.Close();
}
 
Step 2: Add a Image Control in ItemTemplate:
 
<ItemTemplate>
  <asp:Image ID="Image1" runat="server"
ImageUrl='<%# "Handler.ashx?id=" + Eval("id")%>'/>
</ItemTemplate>
 
//display image from the database using handler
Image2.ImageUrl = "Handler.ashx?id=2";
 
 
 
 
sangeetha simma replied to dipa ahuja on 24-Apr-12 05:09 AM
Thanx for the solution..
this is also possible but i need to do like mycode..already i sent that code..so plz verify mycode once and give me the solution
Anoop S replied to sangeetha simma on 24-Apr-12 05:46 AM
Have a Template Column having an Image element in it and set ImageUrl to an aspx page like below.
<TemplateColumn>
<asp:Image ID="LogoImage" ImageUrl='<%# "GetImage.aspx?IMAGE_ID=" + Convert.ToString(DataBinder.Eval(Container.DataItem, "image_id")) %>' runat="server" ToolTip='<%#DataBinder.Eval(Container.DataItem, "image_name") %>' AlternateText='Image'/> 
</TemplateColumn>

In the .aspx page have the below code in page load.
private const string IMAGE_ID= "IMAGE_ID";
protected void Page_Load(object sender, EventArgs e)
{
int imageId= 0;
if (Request.QueryString.Count > 0)
{
if (Request.QueryString[IMAGE_ID] != null)
{
if (int.TryParse(Request.QueryString[IMAGE_ID], out imageId))
{
LogoManager logoManager = new LogoManager();
byte[] binaryImage = logoManager.GetLogo(logoId);

   System.Drawing.Image uploadedImage = System.Drawing.Image.FromStream(new System.IO.MemoryStream(binaryImage));
                        if (uploadedImage != null)
                        {
                            int ImageWidthInPixels = uploadedImage.Width;
                            int ImageHeightInPixels = uploadedImage.Height;
                            //Bitmap 
if (ImageWidthInPixels > Settings.Default.MaxImageWidth) ImageWidthInPixels = Settings.Default.MaxImageWidth;
if (ImageHeightInPixels > Settings.Default.MaxImageHeight) ImageHeightInPixels = Settings.Default.MaxImageHeight;
                            Bitmap b = new System.Drawing.Bitmap(ImageWidthInPixels,ImageHeightInPixels, PixelFormat.Format24bppRgb);//" the size of background. The picture can't be larger than it.
                            Graphics g = Graphics.FromImage(b);
                            g.Clear(Color.White);
                            g.DrawImage(uploadedImage, 0, 0, ImageWidthInPixels, ImageHeightInPixels);
                            Response.ContentType = "image/jpeg";
                            b.Save(Response.OutputStream, ImageFormat.Jpeg); //Use this line, you can output the image to page.
                            b.Dispose();
                        }

}
}
}
}

sangeetha simma replied to Anoop S on 24-Apr-12 06:00 AM
thanx...

cant we change binary data to image using filestream,and that image bind to gridview using gridview row data bind not using any path...