My code is
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source=" & My.Settings.bowlingballdatabase
Dim selectString As String = "Select Picture From BowlingBall WHERE LName = 'Smith'"
Dim oleConnect As New OleDb.OleDbConnection
oleConnect.ConnectionString = dbProvider & dbSource
oleConnect.Open()
Using oleDBCmd As OleDb.OleDbCommand = oleConnect.CreateCommand()
oleDBCmd.CommandType = CommandType.Text
oleDBCmd.CommandText = selectString
Using oleDbReader As OleDb.OleDbDataReader = oleDBCmd.ExecuteReader()
oleDbReader.Read()
Dim ImageBytes As Byte() = CType(oleDbReader(0), Byte())
Dim ms As New MemoryStream(ImageBytes)
Dim img As Image = Image.FromStream(ms)
Me.PictureBox1.Image = img
End Using
End Using
oleConnect.Close()
I think its a problem with the SQL commands maybe? I get the error "Parameter is not valid" on the following line:
Dim img As Image = Image.FromStream(ms)
There is nothing wrong with your code per se . Your problem is almost certainly related to the way Access stores images when they are embedded from within the Access application itself. In those cases an OLE "wrapper" is added to the raw image data before it is stored in the table. This works okay when handling images from within Access , but it can cause problems when working with those images from external applications like your .NET project.
Your code retrieves the OLE-wrapped image object from the Access database and tries to create a .NET Image
object with it. The problem is that while the Image
object can recognize a variety of image types (eg, bitmap, JPEG, etc.) the "OLE-wrapped image object format" is not one of them. Therefore you need to remove the OLE wrapper from the stream of bytes before passing it to Image.FromStream()
.
For more details see my other answer here , which shows how to "unwrap" objects using code from an earlier answer here .
when you do oleDbReader(0), you get the field name of the first field in the returned reader object. use oleDbReader(0)(0) to get the first item in the first column, you know how it goes.
Dim ImageBytes As Byte() = CType(oleDbReader(0)(0), Byte())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.