473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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.
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.
Oct 8 '09 #1
3 8725
Plater
7,872 Expert 4TB
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)
Oct 8 '09 #2
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);
Oct 8 '09 #3
Plater
7,872 Expert 4TB
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
Oct 8 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mansi | last post by:
I'm trying to automate Excel 2003 from Visual C# .Net. Is it possible to export an picture/image file from c# to excel? If so, how do you do this? Are there any code examples available? ...
1
by: Alec | last post by:
Hi All, I am currently using an unbound OLE object to display images stored in a folder. I want to be able to print these images from their normal format (it just happens that these are A4...
4
by: David | last post by:
hello to everybody... in of directories on my pc i have a list of su-directories, and in each directory theres an image "picture.ico"... so then my program is starting to run, i add a new...
4
by: NASAdude | last post by:
I'm working on distributing a database using VS Tools for Office, and am having trouble with linked images on forms/reports. The image objects have a default path\file set on the .Picture property,...
0
by: Stan SR | last post by:
Hi, I use a repeater that calls a page where I display an image (sorry for my english ;) ) This page performs a webRequest to capture a jpg file And with the result I resize the picture and...
3
by: OptimalDesigns | last post by:
I'm trying to convert my website onto my CD drive (F:). Images are giving me problems due to lack of knowledge for sure. My image url looks like this ......
2
Ali Rizwan
by: Ali Rizwan | last post by:
Hi, I want to retrieve pictures from imagelist to a image control. ow can i do this in vb6. i tried but i cannot do it.
5
by: Gilberto | last post by:
Hello Ive been trying to find a way to reduce database size (image related) and the only solution ive found (http://support.microsoft.com/default.aspx?scid=kb;en-us;285820) only deals with...
1
by: sop23456 | last post by:
Microsoft Access 2003 I have hyperlink the image/picture to another file. When I click on the hyperlink, the image appears using MS Paint. What do I do to view Image/picture in Windows Picture...
0
by: Zainull | last post by:
Hi, I'm very new to VB and I am using visual basic 2010 express. I have to write a program that is capable of simple image processing: The user must be able to type in the path and name of an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.