Thursday, August 8, 2013

STORE IMAGES IN DATABASE



STORE IMAGES IN DATABASE 

‘To show an image in picture box
PictureBox1.Image = Image.FromFile(<<file name with full path>>)

‘To Store an image in database table (sql server)  (In sql server use data type ‘Image’)
Dim picBytes As Byte() = Nothing
Dim memStream As New IO.MemoryStream
PictureBox1.Image.Save(memStream, Drawing.Imaging.ImageFormat.Jpeg)
picBytes = memStream.ToArray()

SqlCmd.CommandText = "Insert Into ImageTable(Picture) Values (@pic)”
SqlCmd.Parameters.AddWithValue("@pic ", picBytes)
SqlCmd.ExecuteNonQuery()

‘To Retrieve an image from sql server database and shown in a Picture box

Dim picBytes As Byte() = CType(DataRow.Item("Picture"), Byte())
Dim memStream As IO.MemoryStream = New IO.MemoryStream(picBytes)
PictureBox1.Image = Image.FromStream(memStream)