I want to store an image of PictureBox to SQL Server,What should i .. RRS feed

  • Question

  • hi guys what's wrong with the code below that the "pic" always get "0" in it?I should tell you all that for default the picture box have an image in SQL the field "fldPicture" is in varbinary type.

    Code Snippet

                       MemoryStream stream = new MemoryStream();
                       pictureBox.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                        byte[] pic = new byte[stream.Length];
                       stream.Write(pic, 0, (Int32)stream.Length);     
                        command.Parameters.AddWithValue("@Picture", pic);

    Wednesday, May 7, 2008 7:50 AM

All replies

  • You haven't rewound the stream, so it is still at the end; actually, since you have MemoryStream, you can just use .ToArray() to get the correct buffer.


    For large BLOBs, see here.


    Wednesday, May 7, 2008 9:12 AM
  • Note: you are also using Write when you should be using Read (but ToArray is still simpler...)


    Wednesday, May 7, 2008 9:50 AM