468,134 Members | 1,229 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,134 developers. It's quick & easy.

How to display multiple images from sql server 2005 into c# at runtime.

1
I STORED MULTIPLE IMAGES IN MY SQL SERVER 2005. NOW I WANT TO RETRIEVE PARTICULAR CATEGORY OF IMAGES INTO MULTIPLE PICTUREBOX AT RUNTIME IN C# WINDOWS APPLICATION. i used the following code in my final result form at form_Load using c# windows apps.. But it displays only one picture at run time pls help to me...


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. SqlConnection con = new SqlConnection(@"Data Source=ttt-PC;Initial Catalog=Query_Image;Integrated Security=True");
  5.             SqlCommand cmdnew = new SqlCommand("select pic from Image_Category where IMG_Cat='" + searchvalue.ToString() + "'", con);
  6.             con.Open();
  7.           //  SqlDataReader dr = cmdnew.ExecuteReader();
  8.             SqlDataAdapter da = new SqlDataAdapter(cmdnew);
  9.             DataSet ds = new DataSet("Image_Category");
  10.             byte[] mydata = new byte[0];
  11.             da.Fill(ds, "Image_Category");
  12.             DataRow myrow;
  13.             //if (dr.HasRows)
  14.             //{
  15.             //    while (dr.Read())
  16.             //    {
  17.  
  18.             for (int i = 0; i < 2; i++)
  19.  
  20.             // while (i <= dt.Rows.Count)
  21.  
  22.             {
  23.  
  24.                 myrow= ds.Tables["Image_Category"].Rows[i];
  25.                 mydata = (byte[])ds.Tables[0].Rows[i]["pic"];
  26.                 str[i]=new MemoryStream(mydata);
  27.  
  28.                 pbx[i]=new PictureBox();
  29.                  pbx[i].Size = new Size(150, 150);
  30.                  pbx[i].SizeMode = PictureBoxSizeMode.StretchImage;
  31.                 pbx[i].BackgroundImage = Image.FromStream(str[i]);
  32.                  pbx[i].Visible = true;
  33.                  //this.Controls.Add(pbx[i]);
  34.                 }
  35.  
PLEASE TELL IS IT ANY WRONG WITH THIS CODE?
OR SUGGEST NEW CODE TO ME PLEASE..
Mar 26 '13 #1
3 4474
vijay6
158 100+
Hey rajmca, your code is right but you forgot to use the PictureBox location property. So by default all the PictureBoxes uses same location (One above one). So that only you're seeing only one PictureBox (the PictureBox which created atlast) at runtime. Include these lines and try,

Expand|Select|Wrap|Line Numbers
  1. // Above for loop
  2.  
  3. int x = 10;
  4. int y = 10;
  5.  
  6. // Inside for loop
  7.  
  8. pbx[i].Location = new Point(x, y);
  9. x += pbx[i].Location.X + pbx[i].Width;

And one more thing is you've to use Image property not BackgroundImage property in line #31 in your code.

Expand|Select|Wrap|Line Numbers
  1. //pbx[i].BackgroundImage = Image.FromStream(str[i]); // Instead of this line use the following line.
  2. pbx[i].Image = Image.FromStream(str[i]);
Apr 3 '13 #2
Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte())
stream.Write(image, 0, image.Length)


you can retreve each image and put it into bitmap.

http://net-informations.com/vbprj/da...ieve-image.htm

helsy
Apr 23 '13 #3
getting error on pbx n str so wht to do...i want to retreive multiple images in picturebox from database when the form is loaded..
Aug 7 '16 #4

Post your reply

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

Similar topics

6 posts views Thread by Richard | last post: by
27 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.