Click here to Skip to main content
16,016,678 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
hi,

I would like to show the binary image which is in SQL 2008 database.
I would like to fetch this image and show in image control in ASP.NET.

Thank you.
Posted
Updated 17-Nov-11 2:09am
v2
Comments
[no name] 17-Nov-11 8:09am    
Good for you. What have you tried? Have you done any research? Perhaps stumbled on the hundreds of results on Google?

Either you need to store that Byte[] in file or use response object

1. Use System.IO.file.WriteAllByte(Byte[]) and then give that to picture control
2.
C#
Response.ContentType = "image/jpg";
Response.BinaryWrite((Byte[])objReader["image"]);
 
Share this answer
 
you will have to create a new page called Picture.aspx

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
  int ImageID = DropDownList1.SelectedIndex; 
  Image1.ImageURL = "Picture.aspx?ImageID=" + ImageID;
}



in picture.aspx pageload

C#
protected void Page_Load(object sender, EventArgs e) 
{

  if (Request.QueryString["ImageID"] != null) 
  {

    int ImageID = Convert.ToInt32( Request.Params["ImageID"]);
    using(SqlConnection myConnection = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Inetpub\\wwwroot\\WebserviceUpload2\\App_Data\\Database.mdf;Integrated Security=True;User Instance=True")) 
    {

      const string SQL = "SELECT [MIMEType], [Image] FROM [PictureTable] WHERE [ImageID] = @ImageID";

      SqlCommand myCommand = new SqlCommand(SQL, myConnection);myCommand.Parameters.AddWithValue("@ImageID", ImageID); 
myConnection.Open();

      SqlDataReader myReader = myCommand.ExecuteReader(); if (myReader.Read()) 
      {

        Response.ContentType = myReader["MIMEType"].ToString();
        Response.BinaryWrite((Byte[])myReader["image"]); 
        //Response.Pics(
      }
      myReader.Close();
      myConnection.Close();
    }
  }
}



mark as answer is solves your problem, :)
 
Share this answer
 
First You Have To Retrive Byte[] of a image from database,
And Create A file,write that Byte[] in to that file (.jpg) and Give that file address URI to Image tag.
As sample code
C#
 SqlConnection c = new  SqlConnection("Your SQL Connection String")
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = c;
                cmd.CommandText = "Your Select Statement here to bring image" 
                try
                {
                    c.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        byte[] Uimage = (byte[])dr[0];
//                   Differentiating images With the Session Id's
                        UserImageTempPath = @"\Path" + SessionId + ".jpg";
//                     To get Actual path of the Image folder
                        string path = Server.MapPath("") + UserImageTempPath;
                        FileStream fs = File.Create(path);
                        if (fs.Length != 0)
                        {
                            File.Delete(path);
                            fs = File.Create(path);
                        }
                        fs.Write(Uimage, 0, Uimage.Length);
                        fs.Close();

                    }
                }
                catch { }
                finally { c.Close(); }

            }
            
            HtmlImage i1 = (HtmlImage)this.FindControl("image1");
            HtmlImage i2 = (HtmlImage)this.FindControl("image2");
            i1.Attributes.Add("src", UserImageTempPath);
            i2.Attributes.Add("src", UserImageTempPath);
 
Share this answer
 

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