By Marlon

2010-03-10 11:37:13 8 Comments

I'm working on redeveloping an Access based system into, however when MS went from office 2003 to office 2007 they removed the picture editor within access - which meant that previously stored pictures would no longer display in the system. The guys at the company did a hack that basically saved the images with VBA using excel in the background (I can get more information if you need it) but basically it meant the access image controls could still be used (Object bound frames).

However, I now have the problem of trying to display these in .NET applications, and after countless days of trying different ways of manipulating the byte array I'm close to giving up. I have tried at least 8 different suggested solutions and each one ends with a 'Parameter not recognised' exception when doing Image.fromStream(). Below is the code which has got me the closest so far:

    private void imageExtractTest()
        LogOnDataSetTableAdapters.QueriesTableAdapter qa =
            new LogOnDataSetTableAdapters.QueriesTableAdapter();

        object docO = qa.GetLogonImage();
        if (docO == null || !(docO is byte[]))
        byte[] doc = (byte[])docO;

        MemoryStream ms = new MemoryStream();
        ms.Write(doc, 0, doc.Length);
        int firstByte;
        int secondByte;
        ms.Seek(0, SeekOrigin.Begin);
        firstByte = ms.ReadByte();
        secondByte = ms.ReadByte();

        if (firstByte != 0x15 && secondByte != 0x1C)
            //ErrorResponse("Stored object is not an Access File.");

        int fileTypeLoc = 20; // begin of the file type
        short offset; // end of the file type

        byte[] buffer = new byte[2];
        ms.Read(buffer, 0, 2);
        offset = BitConverter.ToInt16(buffer, 0);

        long seekTotal = 0;
        seekTotal += offset;

        string docType = String.Empty;
        for (int i = fileTypeLoc; i < offset; i++)
            docType += (char)doc[i];

        //if I query docType now I get 'Picture\0\0'

        // magic eight bytes 01 05 00 00 03 00 00 00
        ms.Seek(seekTotal, SeekOrigin.Begin);
        buffer = new byte[8];
        ms.Read(buffer, 0, 8);
        seekTotal += 8;

        // Second offset to move to 
        buffer = new byte[4];
        ms.Read(buffer, 0, 4);
        seekTotal += 4;
        long offset2 = BitConverter.ToInt32(buffer, 0);
        seekTotal += offset2;
        ms.Seek(seekTotal, SeekOrigin.Begin);

        // eight empty bytes
        buffer = new byte[8];
        ms.Read(buffer, 0, 8);
        seekTotal += 8;

        // next n bytes are the length of the file
        buffer = new byte[4];
        ms.Read(buffer, 0, 4);
        seekTotal += 4;
        long fileByteLength = BitConverter.ToInt32(buffer, 0);

        // next N bytes are the file
        byte[] data = new byte[fileByteLength];

        // store file bytes in data buffer
        ms.Read(data, 0, Convert.ToInt32(fileByteLength));

        MemoryStream imageStream = new MemoryStream(data);
        Image test = Image.FromStream(imageStream);

This code was adapted from here, I didn't need the various doctypes identification as I'm only dealing with images, however the image type could be any number of things - jpg, bmp, gif, png etc.

I've also tried saving the outputted byte array but I've had no luck viewing that either. But when I point access to the database and get it to view it, everything is fine. Also the .NET Crystal Report designer is able to get these images some how - so they must be in there somewhere...

Has anyone got any ideas?



@Anthony Griggs 2019-05-16 17:24:00

I needed to do the exact same thing for some 1600 object of different extension types. In my case it was a legacy database that had literally been used for decades. Over the years many different types of files had been added via an OLE Object frame. Some of the items that looked like "Images" turned out to be Word documents with an embedded image... no telling what other file types were in there? All I know is that I researched and tried different extraction methods for more than a week. Even all of Steven Leban's extraction tools like OLEtoDisk, A2KExportOLEtoJPEG, and SaveOLEtoBitmap. Each of which would extract some images... but their was no one size fits all... it was a mess!

In the end I ended up performing an automated screen shot of each image via VBA using the method below. While this may not have been the most ideal it worked for every file type. However the screen shot method captures a screen shot of the full screen. Once I had all of them extracted I then had to use PhotoShop to do another automated process to batch crop all of the photos. Not ideal but it worked!

Private Sub CaptureAllImages()
    On Error Resume Next
    Do While Not Me.RecordsetClone.EOF
        Me.Bookmark = Me.RecordsetClone.Bookmark
        Call Pause(2)
        Call SaveClip2Bit("C:\Users\agriggs\Desktop\Parts Images\MasterPart_" & Me.MasterPartNumber & ".bmp")

End Sub

Public Function Pause(NumberOfSeconds As Variant)
    On Error GoTo Error_GoTo

    Dim PauseTime As Variant
    Dim start As Variant
    Dim Elapsed As Variant

    PauseTime = NumberOfSeconds
    start = Timer
    Elapsed = 0
    Do While Timer < start + PauseTime
        Elapsed = Elapsed + 1
        If Timer = 0 Then
            ' Crossing midnight
            PauseTime = PauseTime - Elapsed
            start = 0
            Elapsed = 0
        End If

    On Error GoTo 0
    Exit Function
    Debug.Print Err.Number, Err.Description, Erl
    GoTo Exit_GoTo
End Function

I tried a couple different screen shot modules but found that the SaveClip2Bit worked the best. Lastly I added a common Pause function to insure the image got saved to disk before moving on to the next. As you can imagine 1600 images took a long time to extract but I can now but this project to rest!

@X-Psi 2012-02-10 07:55:37

In my case the following function worked. The data are stored by a VB6 application.

public static byte[] ConvertOleBytesToRawBytes(byte[] oleBytes)
   // The default encoding is in my case - Western European (Windows), Code Page 1252
   return Encoding.Convert(Encoding.Unicode, Encoding.Default, (byte[])oleBytes);

@Mark Elder 2010-11-17 16:22:39

It is not C# code but here is an Delphi example of a way to solve this problem.

It uses the IOLEObject to draw whatever is stored instead of trying to read out the raw data. Steps:

  1. Read the Access Header in front of the OLE Object
  2. Read out the OLE1 stream
  3. Convert the OLE1 stream to an OLE2 IStorage Object
  4. Use OLELoad to "run" the OLE Ojbect.
  5. Call OLEDraw to draw out the image to a canvas of your choice.

@ferosekhanj 2010-07-07 06:24:51

Try this KB article from microsoft. It contains information on how to access the image blob from access and display that in winforms application.

@Marlon 2010-07-07 09:46:16

thanks for the info but I think you misunderstand the problem. That article just explains how to store a image file into a database. Not how to convert an already stored OLE object to a image. Apparently OLE is just magic...

@Stewbob 2010-03-10 15:59:14

Trying to retrieve an MS-access OLE image field from .NET is way more headache than it is worth. There is some good discussion and info about this topic in this post.

Ultimately, your best, and easiest, solution to be successful with this is to use your working viewing method to save these images as separate files, then import those files into the database as BLOB fields, rather than image fields. Then you can easily read them into .NET.

Related Questions

Sponsored Content

37 Answered Questions

[SOLVED] Error - Unable to access the IIS metabase

  • 2012-10-12 13:15:03
  • jjathman
  • 346738 View
  • 825 Score
  • 37 Answer
  • Tags:   c# .net iis

23 Answered Questions

[SOLVED] Sending email in .NET through Gmail

26 Answered Questions

[SOLVED] How can I get the application's path in a .NET console application?

45 Answered Questions

[SOLVED] Deep cloning objects

  • 2008-09-17 00:06:27
  • NakedBrunch
  • 761150 View
  • 2096 Score
  • 45 Answer
  • Tags:   c# .net clone

24 Answered Questions

[SOLVED] Reading settings from app.config or web.config in .net

26 Answered Questions

[SOLVED] Why not inherit from List<T>?

15 Answered Questions

[SOLVED] How do I turn a C# object into a JSON string in .NET?

47 Answered Questions

[SOLVED] How do I update the GUI from another thread?

16 Answered Questions

[SOLVED] Difference between decimal, float and double in .NET?

21 Answered Questions

[SOLVED] .NET String.Format() to add commas in thousands place for a number

  • 2008-09-19 21:26:21
  • Seibar
  • 654328 View
  • 778 Score
  • 21 Answer
  • Tags:   c# .net

Sponsored Content