2

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)
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
DidIReallyWriteThat
  • 1,033
  • 1
  • 10
  • 39

2 Answers2

1

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 (e.g., 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.

Community
  • 1
  • 1
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
-2

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())
user2930100
  • 346
  • 4
  • 10
  • That is not the problem here. `CType(oleDbReader(0), Byte())` will convert the entire object to a `Byte` array, which is what the OP wants. In fact your code will fail because `CType(oleDbReader(0)(0), Byte())` will return a *single Byte* which cannot be implicitly cast to an array. -1 (Sorry.) – Gord Thompson Feb 24 '14 at 12:16
  • I stand corrected and thanks Gord. When a datareader exposes fileds(columns) and rows, I expect it to be in a 2-d array format. I think it's time to take my head off the typed datasets. – user2930100 Feb 24 '14 at 20:34