OLE Stripper








Display an Image (OLE Object) from a Microsoft Access database by using OLE Stripper

In database programming, it happens a lot that you need to bind a picture box to a field with type of photo or image. For example, if you want to show an Employee’s picture from Northwind.mdb database, you might want to try the following code:

picEmployees.DataBindings.Add(“Image”, bsEmployees, “Photo”, true);

This code works if the images are stored in the database with no OLE header or the images stored as a raw image file formats. As the pictures stored in the Northwind database in are not stored in raw image file formats and they are stored as an OLE image documents, then you have to strip off the OLE header to work with the image properly.

Binding imageBinding = new Binding("Image", bsEmployees,
                                   "ImageBlob.ImageBlob", true);
imageBinding.Format += new ConvertEventHandler(this.PictureFormat);

private void PictureFormat(object sender, ConvertEventArgs e)
    Byte[] img = (Byte[])e.Value;
    MemoryStream ms = new MemoryStream();
    int offset = 78;
    ms.Write(img, offset, img.Length - offset);
    Bitmap bmp = new Bitmap(ms);
    // Writes the new value back
    e.Value = bmp;

Fortunately, there are some overload methods in .NET Framework to take care of this mechanism, but it cannot be guaranteed whether you need to strip off the OLE object by yourself or not. For example, you can use the following technique to access the images of the Northwind.mdb that ships with Microsoft Access and they will be rendered properly.

picEmployees.DataBindings.Add(“Image”, bsEmployees, “Photo”, true,
DataSourceUpdateMode.Never, new Bitmap(typeof(Button), “Button.bmp”));

Unfortunately, there are some scenarios that you need a better solution. For example, the Xtreme.mdb database that ships with Crystal Reports has a photo filed that cannot be handled by the preceding methods. For these complex scenarios, you can download the OLEStripper classes from here and re-write the PictureFormat method as it is shown below:

private void PictureFormat(object sender, ConvertEventArgs e)
     // photoIndex is same as Employee ID
     int photoIndex = Convert.ToInt32(e.Value);
     // Read the original OLE object
     ReadOLE olePhoto = new ReadOLE();

     string PhotoPath = olePhoto.GetOLEPhoto(photoIndex);

     // Strip the original OLE object
     StripOLE stripPhoto = new StripOLE();

     string StripPhotoPath = stripPhoto.GetStripOLE(PhotoPath);

     FileStream PhotoStream = new FileStream(StripPhotoPath
                                           , FileMode.Open);

     Image EmployeePhoto = Image.FromStream(PhotoStream);

     e.Value = EmployeePhoto;



Ole and Accessing Files Embedded in Microsoft Access








Ole and Accessing Files Embedded in Microsoft Access

The Microsoft Access team in 1990 needed a way to not only store files in an Access database, but also to present it to the user, with other software installed on the user’s computer. Because back then, there was no reliable way to find out through the OS what software handled what file type, they had to come up with a new solution which was called Object Linking and Embedding (OLE).

How a file is stored inside Microsoft Access (OLE Structure)?

1. Package header
2. Ole header
3. Datablock length
4. Data
5. Metafilepict block
6. Ole footer

The Package Header structure:
A Signature (short): this indicates that the file is a package
The header size (short)
An object type (uint): 0 = linked, 1= embedded, 2 = either
The length of the friendly name in the header (short)
The length of the class name in the header (short)
The offset of the friendly name (short)
The offset of the class name (short)
The size of the object (int)
The friendly name (string, variable length)
The class name (string, variable length)

The Ole Header structure:
The Ole version (uint)
The Format (uint)
The object type name length (int)
The object type name (string, variable length)
The Ole header actually ends with 8 empty bytes followed by 4 bytes that make up the length of the Datablock as an int.

The Datablock and Data structures:
Inside the datablock can be the actual file, but also there can be a structured storage. To determine if this is a structured storage, there is actually an 8 byte signature at the start of the storage.

The Metafilepict block
When you work with images, there is another block called the metafilepict block. You are not likely to find a CONTENTS element if you need to read the metafilepict block.

Where is the Metafilepict block?

The start position of the Metafilepict can be calculated by using the following formula:

Metefilepict start position = total Package Header length + total Ole Header length + 8 (empty bytes) + 4 (Data length bytes) + Data length + 45 (Metafilepict header length)

The stream at this position contains the actual image file.

Copyright © All Rights Reserved - C# Learners