Title: Load images from an Access database in C#
Category: database, graphics, image processing, Office
Keywords: database, graphics, image processing, Office, save images, Access, Microsoft Access, image file, picture, image, initialize list, list, ListBox
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);
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 the example to experiment with it and to see additional details.