Search code examples
cms-accessjpegtiffansi-c

Using ANSI C to extract binary image data from database into a .jpg file


I am using an ANSI C compiler (LabWindows/CVI version 2010) to extract integer, string and floating point data from an existing Access 2010 database with good success. However, I have not been able to extract image data stored as long binary data into files (.jpg, or .tiff). I suspect this is not a difficult task once the technique is known, I just do not have experience with the code or technique to do this. Any help would be appreciated.


Solution

  • I would expect that if you retrieve all of the data in the image column successfully, you only need to write it to a file using fwrite().

    The usual access pattern for retrieving long data from a column is to use a function that gets it one chunk at a time. This is how the ODBC function SQLGetData works; you pass it a fixed-length buffer and length, and its return value indicates that the data has been truncated, so you write the data you have to the file and then call it again until it returns a success code.

    Looking at the documentation for National Instruments LabWindows/CVI SQL Toolkit, the DBGetColBinaryBuffer() function can return the code -1, DB_TRUNCATION, which is described as "The buffer passed in to hold a result is not large enough to hold the result. A partial result has been returned in the buffer.".

    I would guess that DBGetColBinaryBuffer() is designed to be used in a loop, and if you do so and write the partial results to a file, you will end up with a valid image.