Connecting Tech Pros Worldwide Help | Site Map

Get image from SQL and put in Picture Box

Newbie
 
Join Date: Oct 2009
Posts: 13
#1: Oct 8 '09
I need to select an image from a SQL server table and then show it in a picture box on a form.
Expand|Select|Wrap|Line Numbers
  1. private void btnSearch_Click(object sender, EventArgs e)
  2.         {
  3.             SearchFunctions sf = new SearchFunctions(txtPracCode.Text,txtMRN.Text, txtFName.Text, txtLName.Text, txtDOB.Text, txtSSN.Text, 0);
  4.             long PersonID = Convert.ToInt32(sf.PersonSearch());
  5.             if (PersonID != 0)
  6.             {
  7.                 sf.SetPersonID(PersonID);
  8.  
  9.                 if (sf.CheckPicture() != 0)
  10.                 {
  11.                     MessageBox.Show("Picture exists");
  12.                     SqlConnection conn = null;
  13.                     SqlDataReader rdr = null;
  14.                     try
  15.                     {
  16.                         conn = new SqlConnection("Server=Global2;DataBase=PictureCapture;Integrated Security=True");
  17.                         conn.Open();
  18.                         string sqlstring = "SELECT BinaryChunk FROM Person_Picture WHERE ID=" + PersonID + ";";
  19.                         SqlCommand cmd = new SqlCommand(sqlstring, conn);
  20.  
  21.                         rdr.Read();
  22.                         if (rdr[0] != DBNull.Value)
  23.                         {
  24.                             pbImage.Image = (rdr[0]);
  25.                         }
  26.                     }
  27.                     finally
  28.                     {
  29.                         if (conn != null)
  30.                         {
  31.                             conn.Close();
  32.                         }
  33.                         if (rdr != null)
  34.                         {
  35.                             rdr.Close();
  36.                         }
  37.                     }
  38.                 }
  39.  
  40.                 else
  41.                     MessageBox.Show("Picture does not exist");
  42.  
  43.             }
  44.             else
  45.             {
  46.                 MessageBox.Show("Patient does not exist");
  47.             }
  48.         }
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's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#2: Oct 8 '09

re: Get image from SQL and put in Picture Box


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)
Newbie
 
Join Date: Oct 2009
Posts: 13
#3: Oct 8 '09

re: Get image from SQL and put in Picture Box


Yes. This seems to work.
Expand|Select|Wrap|Line Numbers
  1.                         byte[] getimage = (byte[])cmd.ExecuteScalar();
  2.                         string strfn = Convert.ToString(DateTime.Now.ToFileTime());
  3.                         FileStream fs = new FileStream(strfn,
  4.                                           FileMode.CreateNew, FileAccess.Write);
  5.                         fs.Write(getimage, 0, getimage.Length);
  6.                         fs.Flush();
  7.                         fs.Close();
  8.                         pbImage.Image = Image.FromFile(strfn);
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#4: Oct 8 '09

re: Get image from SQL and put in Picture Box


Have you considered like this:
Expand|Select|Wrap|Line Numbers
  1. byte[] getimage = (byte[])cmd.ExecuteScalar(); 
  2. MemoryStream ms = new MemoryStream(getimage);
  3. Bitmap myImage=Bitmap.FromStream(ms); 
  4. pbImage.Image = myImage;
  5.  
You may have to tweek the .FromStream parameters(check the overloads) depending on if the data in sql is complete or not
Reply