[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

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

[Load images from an Access database in C#]

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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.