Load images from an Access database in C#

[load images]

The post Save images in an Access database in C# explains how to save images in an Access database. This post explains how to load images from an Access display them.

Like previous examples, this program uses a SQL statement of the form SELECT * FROM Books WHERE... to select a record from the database. It uses the following code to display the image stored in the returned record’s CoverImage field.

Bitmap bm = BytesToImage((byte[])reader["CoverImage"]);
picCover.Image = bm;

This code simply fetches the data in the CoverImage field, casts it into an array of bytes, and passes it to the BytesToImage method to convert the bytes into a Bitmap. The following code shows the BytesToImage method.

// Convert a byte array into an image.
private Bitmap BytesToImage(byte[] bytes)
{
    using (MemoryStream image_stream = new MemoryStream(bytes))
    {
        Bitmap bm = new Bitmap(image_stream);
        image_stream.Close();
        return bm;
    }
}

This method creates a MemoryStream associated with the byte array. It creates a new Bitmap passing its constructor the MemoryStream, and returns the Bitmap.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in database, graphics, image processing, Office and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Load images from an Access database in C#

  1. Edmundo Ralickas says:

    Hi Rod,

    Although the article focus on loading images from an Access database in C#, the file available for download has the same title as the other article, saving images.

    I have not checked the file content.

Leave a Reply

Your email address will not be published. Required fields are marked *