-
March 2nd, 2017, 04:08 AM
#1
Image from Access db
Can someone please help me get an image FROM an Access database INTO my windows forms app? The method of adding images to the database is simply copy/paste from web page into a picture container. In my app i am extracting data from the access database, not changing anything in there.
I have opened a connection to the database and have the 'picture' data in a byte array but then I cant assign it to the picturebox in my vb.net application because (as i rightly or wrongly understand it) "the OLE wrapper must be stripped from the array in order to leave a valid image"?
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
-
March 2nd, 2017, 10:05 AM
#2
Re: Image from Access db
Have you tried binding the picture box to the field in question.
I seem to remember doing it that way back in VB5. Not sure if I have tried it in VB.Net or not
If that doesn't work then it would be good to show how you are saving it.
Sound slike you are using one method to save and another to read and that the two are not compatible.
Always use [code][/code] tags when posting code.
-
March 3rd, 2017, 05:57 AM
#3
Re: Image from Access db
Hi DataMiser,
I haven't tried binding, simply because that's not the way i wanted to do it. Its a bit of a learning thing for me to figure out how to do it, but not getting very far! Once mastered i can then use the technique in other projects.
The field in question is simply an OLE Object field in a MSAccess 2010 database:
The user gets image from internet (right click > copy) and pastes it into that field in a form in the database (right click > paste)
Would have to agree with your comment 'using one method to save and another to read'. However, i'm surprised its proving tricky to solve!
-
March 4th, 2017, 02:03 PM
#4
Re: Image from Access db
The only thing I have done with images in a db under .net is that i wrote a project a few years back where I was putting images into a MySQL database for a customer to use on his web site. I can't even remember the details of how I did it now.
With Access way back when I did save and load images using a bound control, that was a piece of cake in VB5, simply bind the data source to the picture box and execute a query to fill the datasource.
Always use [code][/code] tags when posting code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|