Page 1 of 1
Access database & vb.net picture box
#1
Access database & vb.net picture box
Posted 24 December 2008 - 04:38 AM
hi i am devarsh..
i have a problem here..
i have a form where a user can upload images into access data base..but when i insert this images into my access data base it works fine..
but now the problem occurs when i want to retrieve the stored images from my database to my picture box in doing so..i noticed that the images are stored in long binary file format in the access data base to as to how to convert this binary image file into jpeg file so that i can display images stored into my data base in to my picturebox...
when i used ctype method it gave me an error saying cannot convert byte() into system.image...so now my question is as to how can we convery this binary file back to its original jpeg format..
Any type of help would be appreciated thanx in advance..
i have a problem here..
i have a form where a user can upload images into access data base..but when i insert this images into my access data base it works fine..
but now the problem occurs when i want to retrieve the stored images from my database to my picture box in doing so..i noticed that the images are stored in long binary file format in the access data base to as to how to convert this binary image file into jpeg file so that i can display images stored into my data base in to my picturebox...
when i used ctype method it gave me an error saying cannot convert byte() into system.image...so now my question is as to how can we convery this binary file back to its original jpeg format..
Any type of help would be appreciated thanx in advance..
#4
Re: Access database & vb.net picture box
Posted 01 January 2009 - 02:26 PM
Just for the hell of it, here is how I would retrieve an image from an Access database. First I would create a function that turned the data from a byte format to a Bitmap format using the MemoryStream Class and pass it a byte array containing my image data
Then I would do something like this
Hope that helps
Public Function GetImageFromDB(ByRef imageName As String) As Bitmap
Try
Dim conn As New OleDb.OleDbConnection
Dim cmd As OleDb.OleDbCommand
Dim reader As OleDb.OleDbDataReader
conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\YourDBFile.mdb;User Id=YourUsername;Password=YourPassword;"
conn.Open()
cmd = conn.CreateCommand()
cmd.CommandText = "SELECT YourColumnName FROM YourTable WHERE ColumnName = '" & imageName & "'"
reader = cmd.ExecuteReader
If reader.Read Then
Dim imgByteArray() As Byte
Try
imgByteArray = CType(reader(0), Byte())
Dim stream As New System.IO.MemoryStream(imgByteArray)
Dim bmp As New Bitmap(stream)
stream.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Return Nothing
End Try
End If
reader.Close()
conn.Close()
cmd.Dispose()
conn.Dispose()
Return bmp
Catch ex As Exception
MessageBox.Show(ex.Message)
Return Nothing
End Try
End Function
Then I would do something like this
PictureBox1.Image = GetImageFromDB(TextBox1.Text)
Hope that helps
Page 1 of 1

Reply





MultiQuote




|