Click here to Skip to main content
14,489,891 members
Rate this:
Please Sign up or sign in to vote.
See more:
I got a windows form with different Tab control connected with my database
i am able to store and reteive all values like name ,age ,DOB but i am having problem with storing pictire and retreiving it..
So far with storing to database work fine
i can see after select a picture and load datagrid view i can see my picture,
Here is my code for doing it------

                       //  foreach (DataRow row in dataset.Tables[1].Rows)
                       row[Name] = textboxName.Text;  //Values from my Tabcontrol Text box
                       row["Dob"] = datetimepickerDob.value;;

                      //Now for storing image
                       if (picFoto.Image != null)
                            Bitmap bmp = new Bitmap(picFoto.Image);
                            System.IO.MemoryStream imgMemoryStream = new System.IO.MemoryStream();
                            bmp.Save(imgMemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                            byte[] imgByteData = imgMemoryStream.GetBuffer();
                             row["Foto"] = imgByteData;

Now after this when i go bach ko my gridview which contains all table data i can see photo there...Ok
Now when i want to load this tabcontrol i want to load also this photo in my picturebox.
with other data types its perfectly ok but i am having problem on loading my pic
Here is my Code
Applicant aa = new Applicant();
 //Apllicant is my class containing all member and properties
  //for column items.

 foreach (DataGridViewRow dgvr in dataGridView.SelectedRows)

      DataRowView drv = (DataRowView)dgvr.DataBoundItem;

      aa.m_dr = drv.Row;
     aa.Name = bb.m_dr["Name"].ToString();
     if (aa.m_dr["Foto"] != DBNull.Value)

         //MemoryStream ms = new MemoryStream();
         //Image returnImage = Image.FromStream(ms);
         aa.Foto = (Image)bb.m_dr["Foto"];//After this line i got error message cannot convert System.Byte[] to System.Drawing.Image

datatype for Foto is Image using sqlce
Updated 22-Jan-15 3:00am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Try to use a MemoryStream:

//aa.Foto = (Image)bb.m_dr["Foto"];
byte[] data = (byte[]) bb.m_dr["Foto"];
MemoryStream ms = new MemoryStream(data);
aa.Foto = Image.FromStream(ms);
Member 11000455 22-Jan-15 9:16am
yayy Thankyou so much it are a star :-)
Member 11000455 22-Jan-15 9:18am
But can i ask you something is there a way the image displaying in grid view seems queit large so just for gridview i want smaaler like icon do u know how to do this.....
_duDE 22-Jan-15 9:23am
You can try to convert your bitmap to an icon with "Bitmap.GetHicon()" ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100