Get image from SQL and put in Picture Box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • klharding
    New Member
    • Oct 2009
    • 13

    Get image from SQL and put in Picture Box

    I need to select an image from a SQL server table and then show it in a picture box on a form.
    Code:
    private void btnSearch_Click(object sender, EventArgs e)
            {
                SearchFunctions sf = new SearchFunctions(txtPracCode.Text,txtMRN.Text, txtFName.Text, txtLName.Text, txtDOB.Text, txtSSN.Text, 0);
                long PersonID = Convert.ToInt32(sf.PersonSearch());
                if (PersonID != 0)
                {
                    sf.SetPersonID(PersonID);
    
                    if (sf.CheckPicture() != 0)
                    {
                        MessageBox.Show("Picture exists");
                        SqlConnection conn = null;
                        SqlDataReader rdr = null;
                        try
                        {
                            conn = new SqlConnection("Server=Global2;DataBase=PictureCapture;Integrated Security=True");
                            conn.Open();
                            string sqlstring = "SELECT BinaryChunk FROM Person_Picture WHERE ID=" + PersonID + ";";
                            SqlCommand cmd = new SqlCommand(sqlstring, conn);
    
                            rdr.Read();
                            if (rdr[0] != DBNull.Value)
                            {
                                pbImage.Image = (rdr[0]);
                            }
                        }
                        finally
                        {
                            if (conn != null)
                            {
                                conn.Close();
                            }
                            if (rdr != null)
                            {
                                rdr.Close();
                            }
                        }
                    }
    
                    else
                        MessageBox.Show("Picture does not exist");
    
                }
                else
                {
                    MessageBox.Show("Patient does not exist");
                }
            }
    I am getting the error on the line
    pbImage.Image = (rdr[0]);
    I know that it is because I cannot convert this to an image, but I don't know how to convert it so that it will work.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You SQL is returning a byte array object byte[] right?
    You can create a Bitmap object from that. (I think you might need to create a MemoryStream from the byte[] first)

    Comment

    • klharding
      New Member
      • Oct 2009
      • 13

      #3
      Yes. This seems to work.
      Code:
                              byte[] getimage = (byte[])cmd.ExecuteScalar();
                              string strfn = Convert.ToString(DateTime.Now.ToFileTime());
                              FileStream fs = new FileStream(strfn,
                                                FileMode.CreateNew, FileAccess.Write);
                              fs.Write(getimage, 0, getimage.Length);
                              fs.Flush();
                              fs.Close();
                              pbImage.Image = Image.FromFile(strfn);

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Have you considered like this:
        [code=c#]
        byte[] getimage = (byte[])cmd.ExecuteSca lar();
        MemoryStream ms = new MemoryStream(ge timage);
        Bitmap myImage=Bitmap. FromStream(ms);
        pbImage.Image = myImage;
        [/code]
        You may have to tweek the .FromStream parameters(chec k the overloads) depending on if the data in sql is complete or not

        Comment

        Working...