Loading a Windows PictureBox with Images Stored by Access as OLE Objects


You need to display images from a Microsoft Access database in a PictureBox control.


Strip the OLE image header that Microsoft Access adds to the image.

The sample code contains six event handlers:


Sets up the sample by filling a DataTable within a DataSet with the Categories table from the Microsoft Access Northwind sample database. The CategoryID, CategoryName, and Description fields are bound to TextBox controls. The BindingManagerBase is obtained for the Categories table in the DataSet , a handler is attached to manage the PositionChanged event, and that handler is called to position the display on the first record.


Updates the PictureBox with the image for the current record. This event is raised when the Position property value changes.

The CategoryID for the current record is obtained using the position information in the BindingManagerBase object. A Connection object and Command object are created and used to retrieve the Picture binary field for the category record into a Byte array. A MemoryStream object is created and the image written into it from the Byte array using an offset to strip the OLE image header. The static FromStream( ) method of the System.Drawing.Image class is used to load the image into the PictureBox from the MemoryStream .

Move First Button.Click

Sets the current record of the bound controls to the first record by setting the Position property of the BindingManagerBase object to 0.

Move Previous Button.Click

Sets the current record of the bound controls to the previous record by decrementing the Position property of the BindingManagerBase object by 1.

Move Next Button.Click

Sets the current record of the bound controls to the next record by incrementing the Position property of the BindingManagerBase object by 1.

Move Last Button.Click

Sets the current record of the bound controls to the last record by setting the Position property of the BindingManagerBase object to the total number of records, as returned by the Count property, minus 1.

The C# code is shown in Example 7-29.

Example 7-29. File: DisplayMsAccessImageForm.cs

using System;
using System.Configuration;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Data;
using System.Data.OleDb;

private const int MSACCESSIMAGEOFFSET = 78;

private DataSet ds;
private OleDbDataAdapter da;

private BindingManagerBase bm;

// . . . 

private void DisplayMsAccessImageForm_Load(object sender,
 System.EventArgs e)
 // Create the DataSet.
 ds = new DataSet( );

 // Create the DataAdapter and retrieve the Categories table.
 String selectCommand =
 "SELECT CategoryID, CategoryName, Description FROM Categories";
 da = new OleDbDataAdapter(selectCommand,
 da.FillSchema(ds, SchemaType.Source, "Categories");
 da.Fill(ds, "Categories");

 // Bind table fields to controls.
 categoryIdTextBox.DataBindings.Add("Text", ds,
 categoryNameTextBox.DataBindings.Add("Text", ds,
 descriptionTextBox.DataBindings.Add("Text", ds,

 // Get the binding manager base for the parent table.
 bm = BindingContext[ds, "Categories"];
 // Update the image in response to each record reposition.
 bm.PositionChanged += new EventHandler(bm_PositionChanged);
 // Update the display for the first record.
 bm_PositionChanged(null, null);

private void bm_PositionChanged(Object sender, EventArgs e)
 // Refresh the photo displayed when the current record changes.

 // Get the new CategoryID using the BindingManager.
 int categoryId =

 // Create a connection.
 OleDbConnection conn = new OleDbConnection(
 // Create a command to retrieve the category photo.
 String sqlText = "SELECT Picture FROM Categories WHERE CategoryID=" +
 OleDbCommand cmd = new OleDbCommand(sqlText, conn);

 // Retrieve the image from the database.
 conn.Open( );
 Byte[] image = (Byte[])cmd.ExecuteScalar( );
 // Write to a stream removing the image header.
 MemoryStream ms = new MemoryStream( );
 conn.Close( );

 // Load the image into the PictureBox from the stream.
 picturePictureBox.Image = Image.FromStream(ms);
 ms.Close( );

private void moveFirstButton_Click(object sender, System.EventArgs e)
 bm.Position = 0;

private void movePreviousButton_Click(object sender, System.EventArgs e)
 bm.Position -= 1;

private void moveNextButton_Click(object sender, System.EventArgs e)
 bm.Position += 1;

private void moveLastButton_Click(object sender, System.EventArgs e)
 bm.Position = bm.Count - 1;


The Windows Forms PictureBox control displays bitmap, JPEG, metafile, or icon images.

Microsoft Access stores an image as an OLE object that wraps the actual image. This means that the image is prefixed with a variable-length header that must be stripped off to retrieve the image. The length of this header for bitmap images stored in the Northwind sample database is 78 bytes.

In the solution, the image stored as a BLOB in the database is retrieved into a Byte array. The Byte array is copied into a MemoryStream object using an overload of the Write( ) method that allows the 78 byte offset to be specified. The static FromStream( ) method of the Image class creates an Image object from the MemoryStream that is loaded into the PictureBox .

Connecting to Data

Retrieving and Managing Data

Searching and Analyzing Data

Adding and Modifying Data

Copying and Transferring Data

Maintaining Database Integrity

Binding Data to .NET User Interfaces

Working with XML

Optimizing .NET Data Access

Enumerating and Maintaining Database Objects

Appendix A. Converting from C# to VB Syntax

show all menu

ADO. NET Cookbook
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
ISBN: 0596101406
EAN: 2147483647
Year: 2002
Pages: 222
Authors: Bill Hamilton
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net