11 Replies - 2054 Views - Last Post: 23 September 2014 - 10:56 AM Rate Topic: -----

#1 sang_s  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 43
  • Joined: 09-June 14

Retrieve Image from database and display in the picturebox

Posted 18 September 2014 - 10:08 PM

I am creating an employee details project, the image is getting stored in the database but i am not able display it in the picture box,



private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(Connectionstring);
            con.Open();
            SqlCommand cmd  = new SqlCommand("select photo from emprecordtable where eid='"+comboBox1.Text+"'", con);
            //SqlDataAdapter da = new SqlDataAdapter(cmd);
            //DataSet ds = new DataSet();
            //da.Fill(ds);
            //if (ds.Tables[0].Rows.Count > 0)
            //{
            //    MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["photo"]);
            //    pictureBox1.Image = new Bitmap(ms);
            //}
            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.Read())
            {

                byte[] img_arr1 = (byte[])dr["photo"];
                MemoryStream ms1 = new MemoryStream(img_arr1);
                ms1.Seek(0, SeekOrigin.Begin);
                pictureBox1.Image = Image.FromStream(ms1);// In this line i am getting the error as [b]Parameter Not Valid[/b]
            }
            dr.Close();
            con.Close();

        }








In sql server i have table called emprecordtable and i have the fields eid, rfid, empname, designation, phno, addres, and photo which is the image field and i have given the datatype as image but in the database the image is getting stored as Binary Data.. So please anyone guide me how to display in the picturebox from the databse

Is This A Good Question/Topic? 0
  • +

Replies To: Retrieve Image from database and display in the picturebox

#2 Charles:)  Icon User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: Retrieve Image from database and display in the picturebox

Posted 19 September 2014 - 05:03 AM

What format is the image in before it's saved to the database? BMP? PNG? JPEG? GIF?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,379
  • Joined: 02-June 10

Re: Retrieve Image from database and display in the picturebox

Posted 19 September 2014 - 05:52 AM

I would start by verifying your assumptions.
19 byte[] img_arr1 = (byte[])dr["photo"];
20 MemoryStream ms1 = new MemoryStream(img_arr1);
21 ms1.Seek(0, SeekOrigin.Begin);
22  Image bob = Image.FromStream(ms1);
23 pictureBox1.Image = bob;


I made a minor change at line 22 so you can check each assumption one at a time.

If you put a breakpoint at line 22 you should then be able to look at the actual content of img_arr1 and ms1. Do they have any real content? Or is the array just filled with nulls?

If it looks like the array has genuine content then F10 to see if bob becomes an image.
Was This Post Helpful? 0
  • +
  • -

#4 sang_s  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 43
  • Joined: 09-June 14

Re: Retrieve Image from database and display in the picturebox

Posted 21 September 2014 - 09:56 PM

ok i will just try with these changes
Was This Post Helpful? 0
  • +
  • -

#5 sang_s  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 43
  • Joined: 09-June 14

Re: Retrieve Image from database and display in the picturebox

Posted 21 September 2014 - 10:34 PM

byte[] img_arr1 = (byte[])dr["photo"];
                MemoryStream ms1 = new MemoryStream(img_arr1);
                ms1.Seek(0, SeekOrigin.Begin);
                Image bob = Image.FromStream(ms1);
                pictureBox1.Image = bob;



Image bob = Image.FromStream(ms1)

i am getting the error again in this line as PARAMETER NOT VALID

This post has been edited by Skydiver: 21 September 2014 - 10:55 PM
Reason for edit:: Put code in code tags. Learn to do this yourself..

Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: Retrieve Image from database and display in the picturebox

Posted 21 September 2014 - 10:58 PM

Did you check to see if the contents of the memory stream contains valid data that Image.FromStream() knows how to load?

What is the file format of the data stream?
Was This Post Helpful? 0
  • +
  • -

#7 sang_s  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 43
  • Joined: 09-June 14

Re: Retrieve Image from database and display in the picturebox

Posted 21 September 2014 - 11:19 PM

i dint understand your question
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: Retrieve Image from database and display in the picturebox

Posted 22 September 2014 - 05:24 AM

I just rephrasing the question from post #2. What data format is the image in? Is it a BMP, PNG, JPEG, GIF, or TIFF formatted data stream?
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,379
  • Joined: 02-June 10

Re: Retrieve Image from database and display in the picturebox

Posted 22 September 2014 - 06:31 AM

View Postsang_s, on 21 September 2014 - 11:34 PM, said:

byte[] img_arr1 = (byte[])dr["photo"];
                MemoryStream ms1 = new MemoryStream(img_arr1);
                ms1.Seek(0, SeekOrigin.Begin);
                Image bob = Image.FromStream(ms1);
                pictureBox1.Image = bob;



Image bob = Image.FromStream(ms1)

i am getting the error again in this line as PARAMETER NOT VALID


There are a dozen reasons that could cause this. For starters is there any information in the array? Do you know how to check the values of our variables while debugging? Please show us a screen shot of the Watch window where all these values are displayed so we can see their values.
Was This Post Helpful? 0
  • +
  • -

#10 sang_s  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 43
  • Joined: 09-June 14

Re: Retrieve Image from database and display in the picturebox

Posted 22 September 2014 - 10:29 PM

it is jpeg image
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5928
  • View blog
  • Posts: 20,267
  • Joined: 05-May 12

Re: Retrieve Image from database and display in the picturebox

Posted 23 September 2014 - 05:33 AM

So, if you save that memory stream into a file and try it open it as .JPEG file using Windows Explorer, does it open correctly?
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,379
  • Joined: 02-June 10

Re: Retrieve Image from database and display in the picturebox

Posted 23 September 2014 - 10:56 AM

Its becoming very clear by the total lack of response to my questions asking what are the run-time values of your variables that you must not know.

You really need to take the time to work on some debugging techniques and tutorials. Its going to be near impossible to debug your program if you have no idea how to debug, nor are you even aware if your variables contain ANY information, let alone the EXPECTED information.

See FAQ # 2 below

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2014
Spoiler

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1