OLE OBject problem with Access

kaberto

Member
Joined
Sep 22, 2007
Messages
5
Programming Experience
Beginner
Hi!

I need to load a list of JPEG images from MS Access to VB.Net unfortunately I can't even load a single picture into VB.Net, I can load BMP pictures but not JPEG. I have found a C# code that does what I wanted.

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2156764&SiteID=1

But unfortunately, when I converted the code to VB.Net it does not work.
Upon debugging I found out that the strTemp returns different results in C# and in VB.Net. The VB.Net variable only returns a very small portion of the entire array. With this code I can load BMP images but I can't with JPEG

Here's the converted code

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim BITMAP_ID_BLOCK As String = "Bitmap"
        Dim JPG_ID_BLOCK As String = "\u00FF\u00D8\u00FF"
        'create the command object and store the sql query
        aCommand = New OleDbCommand(("select Image from Images where KeyID = " + TextBox1.Text), aConnection)
        Dim ms As MemoryStream = New MemoryStream
        Dim img() As Byte = CType(aCommand.ExecuteScalar(), Byte())
        Dim u8 As Encoding = Encoding.UTF7
        Dim strTemp As String = u8.GetString(img)
        Dim strVTemp As String = strTemp.Substring(0, 300)
        Dim iPos As Integer = -1
        Dim test As Integer = strVTemp.IndexOf(JPG_ID_BLOCK)
        If (strVTemp.IndexOf(BITMAP_ID_BLOCK) = -1) Then
            ' Find the starting block of the image
            iPos = strVTemp.IndexOf(JPG_ID_BLOCK)
        Else
            iPos = 78
        End If
        ' From the position above get the new image
        If (iPos <> -1) Then 
            ms.Write(img, iPos, (img.Length - iPos))
            Me.PictureBox1.Image = Image.FromStream(ms)
        End If
        ms.Close()
    End Sub
Hope somebody can help. Thanks!
 
Last edited by a moderator:
First off, I would avoid using the Access database to store image. Since the Access database is a file by itself, nothign prevents you from simply writing the files to the file system beside the .mdb file...

I did read a jpg, bitmap, gif, png image from database once and I did not have to use a different implementation for any of the lot. I simply took the byte array, turned it into a stream and use Image.FromStream(). Have you tried to use the whole byte array? Also, try to set the position of the MemoryStream to 0 between filling it and reading it. Otherwise, maybe the problem is when you write the image to the database... Could you show that code too?
 
Thanks for the response,

Unfortunately the databases that I will be using are also used for other purposes using Access, all I did was import the table needed and attach it to VB. I can load the image if I just use the filename and call it from a folder and not load the image from within Access itself but since the databases have about 300~400 images each, It's very time consuming to find what filename is being used by the OLE Object and then make another column for it.

Have you tried to use the whole byte array?

I did, unfortunately I get an invalid parameter issue. Something to do with the OLE Header in Access as they say in other forums.

Otherwise, maybe the problem is when you write the image to the database... Could you show that code too?

I'm sorry but the image was written in Access using just Drag and Drop.

What's really bugging me is that I can make it work in C# but not in VB. upon debugging, the image when encoded in UTF7 is "$\0\0\0\0\b\0\b\0\0\0ÿÿÿÿPackage\0Package\....." in C# but is only "$ in VB.

BTW, can you post a sample code on how you were able to load JPG and BMP files?
 
Last edited:
um.. are you trying to read binary data into a string? DOes this not strike you as something that is intrinsically prone to failure?


Also, did you know that you can use C# code in a VB solution? Put the C# in its own project, compile it and reference it, and it will work from VB. The IDE steps between them without a problem, because they are actually the same language
 
I thought the parsing binary data to string might have been the problem too, but the resulting string is only used to recognize a JPEG from a bitmap and discarded after that.

My guess is that drag and dropping the file in the IDE of MS Access probably stores the filetype, filename or some other info about the image along the binary data of the file. If you could simply store the binary data yourself in the code, the additionnal header would probably disappear along with your problem.

Here's a link with some code : http://forums.devx.com/archive/index.php/t-142085.html
 
um.. are you trying to read binary data into a string? DOes this not strike you as something that is intrinsically prone to failure?


Also, did you know that you can use C# code in a VB solution? Put the C# in its own project, compile it and reference it, and it will work from VB. The IDE steps between them without a problem, because they are actually the same language

Sorry, I'm a newb and it did work in C# so I thought it was a nice start.

Also sorry for asking but how do you reference C# code into VB?
 
http://www.codeproject.com/cs/database/AccessBlob.asp

Again, this is C# but converters exist to change it to vb.net. Additionally, to reference a C# project in VB you must have:

Visual Studio
Right click the solution (solution explorer, show all files ON) and choose Add Project
Add the c# project
It can now be referenced and used by VB.NET


You may find other articles that discuss BLOBs in Access, with VB.NET
 
Back
Top