none
C# Retrieving Image OLE Binary Data from Access Database RRS feed

  • Question

  • Hi, I've inserted a bitmap byte array data into an Access database OLE object field using C# without any problems. However, when I tried to extract the same OLE object from an Access database file and saving it as a .bmp, I've reached the problem where most of the binary data is missing. The extracted binary image file has headers and footer same as the inserted image.

    Here is a snippet of the insertion code:

    string sQry = "INSERT INTO Image(userid,Right_Image)VALUES(?,?)";
                OleDbCommand aCommand = new OleDbCommand(sQry, aConnection);
                aCommand.Parameters.Add("@userid",OleDbType.VarChar,15).Value = sUserID.Trim();       
                aCommand.Parameters.Add("@Right_Image",OleDbType.Binary).Value = byteArray;

    Both inserted and retrieived files have binary data up to 436 characters, they both start with:
    BM6´......6...(...€...à...............Ä...Ä..............ÿ...ÿ...ÿ...ÿ...ÿ...ÿ...ÿ...ÿ...ÿ...ÿ..

    And then at the end of the file they have the same characters:
    Ì..n|¡F1.02.05...MP201208RB202824.J....gegfggkjkjhmkhjmpjmknoqpomolnopo

    The retrieved file has empty spaces besides these two parts that are same as the inserted file.

    Here's a snippet of my retrieval code:
    string sQry = "SELECT Right_Image FROM Image where USERID=@USERID";
    OleDbCommand aCommand = new OleDbCommand(sQry, aConnection);
    aCommand.Parameters.Add("@userid",OleDbType.VarChar,15).Value = sUserID.Trim();
    try
        {
            aReader = aCommand.ExecuteReader(CommandBehavior.Default);
            if(aReader.Read())
            {
                if (!aReader.IsDBNull(0))
                {    
                    aReader.GetBytes(0,0,rightEyeRightIllumIrisCode,0,rightEyeRightIllumIrisCode.Length);
                }
            }
        }

    Also when I opened the database file and tried to double click on the OLE object field, it gives me the error:
    “A problem occurred while Microsoft Access was communicating with the OLE server or ActiveX Control.
    Close the OLE server and restart it outside of Microsoft Access. Then try the original operation again in Microsoft Access. “
    I don't know if this has to do with my C# output.

    Any help is greatly appreciated. THANK YOU!

    Monday, November 26, 2012 9:32 PM

Answers

  • Hi,

    I solved the problem. The problem was that I was inserting a raw image byte array data to the database, and outputted a converted raw image to bmp to disk. That's why when I read from the database I'm getting the raw info. After converting the retrieved data to bmp I was able to see the image! Thanks for the help, I wouldn't have noticed the issues without you guys.

    • Marked as answer by nejidoreb1 Tuesday, November 27, 2012 5:53 PM
    Tuesday, November 27, 2012 5:14 PM

All replies

  • If you want Access to open the image in structured storage way, you got to add the OLE header so that Access can recognize the data as a file package. Access adds the header automatically when you save the data using VBA, if you really have some time to spare and want to write the header by yourself,  check [MS-CFB]: Compound File Binary File Format.

    Or you can just forget opening the field in Access itself and always ask the user to use your app to open the database, then you don't need the OLE header.



    Visual C++ MVP

    Monday, November 26, 2012 11:26 PM
  • The parameter data type should be OleDbType.LongVarBinary when inserting BLOB data. Also, you cannot view this data from Microsoft Access.

    In addition, I don't see the code, but I assume that *all* of the data is being read into byteArray.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, November 27, 2012 2:26 PM
  • Hi,
    I just tried to insert the image as OleDbType.LongVarBinary. I figured the problem for the missing data was that my byte array initialization size was 1024, and I only used GetBytes once. I am fixing that by reading all the data in chunks and the output file size totalled 307200. Now I am getting more data in the retrieved file but is still missing some at the top after the bmp header information. The inserted image's on the harddrive was 301 KB (308,278 bytes); I checked the code and the byte array initialization was 307200 for the image inserted. I changed that size afterwards to [308278] but the retrieved data is still 307200. Do you have any thoughts about this?

    Thanks for your help!

    Tuesday, November 27, 2012 4:48 PM
  • Hi,

    I solved the problem. The problem was that I was inserting a raw image byte array data to the database, and outputted a converted raw image to bmp to disk. That's why when I read from the database I'm getting the raw info. After converting the retrieved data to bmp I was able to see the image! Thanks for the help, I wouldn't have noticed the issues without you guys.

    • Marked as answer by nejidoreb1 Tuesday, November 27, 2012 5:53 PM
    Tuesday, November 27, 2012 5:14 PM
  • Can you post the code you used to populate byteArray for the INSERT statement? Also rightEyeRightIllumIrisCode should be initialized to the value of aReader.Item(0).Length when reading the bytes from the database into the array:

    fieldLen = aReader.Item(0).Length;
    byte[] rightEyeRightIllumIrisCode = new byte[fieldLen];


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, November 27, 2012 5:19 PM
  • Ah, OK then ignore my last post.

    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, November 27, 2012 5:22 PM