MS Access images to WinForms application

DavyEFC

Well-known member
Joined
Dec 17, 2010
Messages
51
Programming Experience
5-10
I've been searching for a few days now and getting nowhere - or going round in circles! Same question asked in a lot of places but no real answer that nails it (that i can see)

I have an MS Access 2010 database and one of its fields is set as OLE object and has pictures in it. How can I convert the picture stored in the OLE object to a regular picture (jpg, gif or whatever) to use in my Windows Forms application?

- There is no option not to use the MS Access database as its used for other things
- The pictures have been added to MS Access via paste method
- In my WinForms app I have code that successfully connects to and reads the database fields therein, including retrieving an array of byte that is the OLE object
- I have tried writing that array to a memory stream and generating an image from that stream but no luck

My understanding is that from this array i have to strip the OLE wrapper, but that's where my understanding ends, unfortunately.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,518
Location
Lansing, MI; USA
Programming Experience
10+
What I have always done is write 2 applications for this, one is the main application the user(s) get and does the reading of the images from the DB (as well as reading/writing to the database as needed) and the 2nd application for reading/writing to the database, even all of the images. By doing this you're doing a standard insert of the binary image data via .Net into the database, so all the other application needs to do is ready it and convert it back to an image object that gets displayed on the form(s).
 

DavyEFC

Well-known member
Joined
Dec 17, 2010
Messages
51
Programming Experience
5-10
JuggaloBrotha - thanks for the reply, but doesn't really answer my question!
I've no option not to use the MS Access database as its used for other things. How can I convert the picture stored in the OLE object to a regular picture? In my WinForms app I have code that successfully connects to and reads the database fields therein, including retrieving an array of byte that is the OLE object.
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,518
Location
Lansing, MI; USA
Programming Experience
10+
Oh sorry, I thought you already had that worked out, I misread your first post.
What you do is read the image into a byte array then assign that array to your field for the image.

That sounds awfully vague, but I don't know how in .Net you're doing your database things, are you using the DataSet file in your project and calling a TableAdapter method? Are you declaring all the ADO.Net stuff in your own code (creating an OleDbConnection and OleDbCommand objects then calling the ExecuteNonQuery() on the command) ?
 

DavyEFC

Well-known member
Joined
Dec 17, 2010
Messages
51
Programming Experience
5-10
Hey JuggaloBrotha, no worries on the misreading, I appreciate you're trying to help me!
I already have the data in a byte array but then I cant assign it to the picturebox in my vb.net application because (as i understand it) the OLE wrapper must be stripped from the array in order to leave me with a valid image.

To be clear, in my .net app i am tapping into the access database, not changing anything in there, just reading data. I have opened a connection to the database then i'm doing this:

Code:
        Dim dA As OleDb.OleDbDataAdapter
        Dim dS As DataSet
        Dim SQL$
        '------------------------------------------
        SQL = "SELECT * FROM tblMovies WHERE [MName] = '" & N & "';"
        dA = New OleDb.OleDbDataAdapter(SQL, dbCon)
        dS = New DataSet
        dA.Fill(dS, "tblDS1")
        dA = Nothing

                Dim mCover() As Byte
                Dim ms As MemoryStream
                Dim img As Image
                Try
                    mCover = dS.Tables("tblDS1").Rows(0).Item(1) '=array of bytes
                    ms = New MemoryStream()
                    ms.Write(mCover, 78, mCover.Length - 78) 'write mcover to ms without 1st 78 bytes??
                    img = Image.FromStream(ms) 'make image from stream <- error here (Parameter is not valid)
                    frmMain.pbxCover.Image = img 'display
                Catch ex As Exception

                End Try

        dS = Nothing
 

DavyEFC

Well-known member
Joined
Dec 17, 2010
Messages
51
Programming Experience
5-10
Still nobody able to help with this? I tried to follow jmcilhinney's example here: Saving Images in Databases It looked simple enough but no joy. The method of adding images to the database is copy/paste from web page.

Error is: An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll - Additional information: Parameter is not valid.
On the line: Pic = Image.FromStream(memStream)

Code:
    Sub GetDBData(N$)
        Dim dbCon As New OleDb.OleDbConnection
        Dim dA As OleDb.OleDbDataAdapter
        Dim dS As DataSet
        Dim dbIsOpen As Boolean
        Dim SQL$
        Dim RCount%
        '--------------------------------------
        Call ClearDBFields()
        frmMain.txtmName.Text = N

        Try
            dbCon.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0; Data Source = " & Application.StartupPath & "\Movies.accdb"
            dbCon.Open()
            dbIsOpen = True
        Catch ex As Exception
            dbIsOpen = False
            MsgBox("Error opening db!", 16, c_AppName)
        End Try

        If dbIsOpen = True Then
            SQL = "SELECT * FROM tblMovies WHERE [MName] = '" & N & "';"
            dA = New OleDb.OleDbDataAdapter(SQL, dbCon)
            dS = New DataSet
            dA.Fill(dS, "tblDS1")
            dA = Nothing
            RCount = dS.Tables("tblDS1").Rows.Count - 1

            If RCount = 0 Then
                With dS.Tables("tblDS1").Rows(0)

                    Dim arrBytes As Byte() = .Item(1)
                    Dim Pic As Image = Nothing
                    Using memStream As New MemoryStream(arrBytes)
                        Pic = Image.FromStream(memStream)
                    End Using
                    frmMain.pbxCover.Image = Pic

                    frmMain.chkmSeen.Checked = .Item(6)
                    If Not IsDBNull(.Item(9)) Then frmMain.txtmAdded.Text = .Item(9)
                    If Not IsDBNull(.Item(3)) Then frmMain.txtmCat.Text = .Item(3)
                    If Not IsDBNull(.Item(4)) Then frmMain.txtmStars.Text = .Item(4)
                    If Not IsDBNull(.Item(5)) Then frmMain.txtmPlot.Text = .Item(5)
                End With
            Else
                frmMain.txtmName.Text = "(Not Found)"
            End If

            dS = Nothing
        End If

        dbCon.Close()
    End Sub
 
Top Bottom