Save Microsoft Access database OLE Objects (jpegs) to disk via VB.

D

dmbuso

I have an Access database with a table named 'tblMedia'. In tblMedia I have a
field named 'Media' that is defined as an 'OLE Ojbect' field. In this Media
field, I have pictures, they are jpegs. I'm using VB.NET 2005 and I would
like to open the Access database and write the OLE Objects to the file system.

Here's what I got and it's not working.

'Open connection to the Access database
OpenConnection()

Dim Command As New OleDbCommand("SELECT * FROM TBLMEDIA WHERE
[MediaType]='Mug shot'", _DBConn)
Dim Reader As OleDbDataReader = Command.ExecuteReader()
Dim picture As Image = Nothing

'Read each line of TBLMEDIA and write out to Media file
While Reader.Read
'Place image on the clipboard
Dim pictureData As Byte() = CType(Reader.GetValue(3), Byte())

Using stream As New IO.MemoryStream(pictureData)
picture = Image.FromStream(stream)
End Using

My.Computer.Clipboard.SetImage(picture)

'Get the image from the clipboard
If My.Computer.Clipboard.ContainsImage() Then
picture = My.Computer.Clipboard.GetImage

'Write the image to a disk file
picture.Save(Me.txtDestFolder.Text & "\" &
Reader.GetValue(1).ToString & ".jpg")
End If
End While


I get a 'Parameter is not valid.' error on this line above:
picture = Image.FromStream(stream)
 
W

WenYuan Wang [MSFT]

Hello Dave,

I understood you want to get images from MS Access database, but the
application failed with the line "Image.FromStream(stream)". The error
message is "Parameter is not valid". If I misunderstood anything here,
please correct me.

The array of bytes you received from OLE Ojbect filed contains a 78-byte
prefix that has nothing to do with the image. Those bytes are just the
header created when the image was added as an OLE object to MS Access. It
must undergo the following modification to work with the OLE field of MS
Access database:

Using stream As New IO.MemoryStream()
stream.Write(pictureData,78, pictureData.Length - 78 )
picture = Image.FromStream(stream)
End Using

Hope this helps. Please try this method and let me know the result. If you
face any further issue, feel free to update here again. We are glad to
assist you.

Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dmbuso

Wen,

I commented out my code and pasted yours in and I still get the same error
message "Parameter is not valid".

Any other suggestions?
 
W

WenYuan Wang [MSFT]

Hello Dave,
Thanks for your reply.

"Parameter is no valid" error means the stream which you passed into
Image.FromStream() method isn't pointing to a valid image. Are you sure the
byte array stored in Access Database is pointing to jpeg image?

In order to figure out the root cause, I think it's necessary for me to
reproduce this issue on side now. Is it possible for you to send me your
sample Access Database? I will try it on my side, and perform further
research. My alias is (e-mail address removed). I'm waiting for your reply.

Have a great day,
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

dmbuso

WenYuan,

I created a sample Access database, I then inserted 5 different .jpg
pictures into the OLE Object field for a total of 5 records. Looking at my
code from my first post, I created a 'MediaType' field and set the values to
'Mug shot'. I also made sure that the OLE Object field was the 4th field
defined in my table. I also named the table 'tblMedia' since that is what the
code is referring to.

I ran my code against the new sample database and I received the same error
('Parameter is not valid.')

If you want I could e-mail you the sample database and my code but you could
very easily reproduce the error just like I did.

If you want me to e-mail you my stuff, please give me an alternate e-mail
address. I've already tried to email you at '(e-mail address removed)'.
Outlook gives me this error message:

Your message did not reach some or all of the intended recipients.

Subject: Test Message
Sent: 12/13/2007 9:28 AM

The following recipient(s) could not be reached:

(e-mail address removed) on 12/13/2007 9:28 AM
The message could not be delivered because the recipient's
destination email system is unknown or invalid.
 
W

WenYuan Wang [MSFT]

Hello Dave,
Thanks for your reply,

It seems you send the mail to a wrong address.
My alias is (e-mail address removed). I'm waiting for your reply.
As we discussed before, you can get me at (e-mail address removed).

I created an Access DB, and insert some jpeg images into it. Finally, I
reproduced it.
After research, I found 78 byte is used for BMP image. It doesn't work fine
with JPEG image file. Under my test, the offset for JPEG image is 189.but
I'm not sure. It seems the number dependens on the size of file.I need
perform further research.

Is it possible for you to give me your email Address? Thereby, I can pass
the information for you as soon as possible.(again, my alias is
(e-mail address removed). You can send an email to me.) If you have any more
concern, please also feel free to update here. We are glad to assist you.

Have a great day.
Best regards,

Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Marc

WenYuan,

I have the same problem that is 'parameter not valid'. Do you have any
developments on the offset value for JPEG images? How do you determine if the
offset value is 189 or something else?

Cheers,
Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top