Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / March 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dmbuso - 11 Dec 2007 03:23 GMT
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)

Signature

Dave B.

WenYuan Wang [MSFT] - 11 Dec 2007 09:01 GMT
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.
dmbuso - 12 Dec 2007 16:02 GMT
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?
Signature

Dave B.

> Hello Dave,
>
[quoted text clipped - 25 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
WenYuan Wang [MSFT] - 13 Dec 2007 08:57 GMT
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 v-wywang@microsoft.com. 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.
dmbuso - 13 Dec 2007 14:47 GMT
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 'v-wywang@online.microsoft.com'.
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:

     v-wywang@online.microsoft.com on 12/13/2007 9:28 AM
           The message could not be delivered because the recipient's
destination email system is unknown or invalid.

Signature

Dave B.
David.M.Busovicki@usdoj.gov
(814) 532-4925

> Hello Dave,
> Thanks for your reply.
[quoted text clipped - 15 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
WenYuan Wang [MSFT] - 14 Dec 2007 10:54 GMT
Hello Dave,
Thanks for your reply,

It seems you send the mail to a wrong address.
> My alias is v-wywang@microsoft.com. I'm waiting for your reply.
As we discussed before, you can get me at v-wywang@microsoft.com.

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
v-wywang@microsoft.com. 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.
Marc - 07 Mar 2008 06:19 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.