Chapter 3 Pictures in the Database
The first section deals with BLOBs and stores images in access
Developing database applications these days requires more than just processing text or numeric data. For example, if you are developing Internet/Intranet-based or multimedia-based applications, images in the database must be displayed frequently as well as text in the database.
In the third chapter of this tutorial, we will learn how to retrieve and display graphic data (images) in an Access database through ADO. Don't worry that working with images in an Access database will require more database programming skills than what this tutorial has provided so far. Let us assume that we already know more than we have been given.
If you started from the beginning of this tutorial (especially Chapter 2), you will know how to connect to the database and display the applications table in the DBGrid (from our aboutdelphi.mdb database). Remember, we need three data components: DBGrid, ADOTable, and DataSource to get and display data from the Applications table.
Back in Chapter 1, when we created the database, we left the last field in the Applications table blank (we filled in the other fields with dummy metadata). The field name of the last field is Picture, and its type is OLE object type.
If you scroll right to the last column of the DBGrid, you will see something like this:
When using MS Access, we can store images (and other big data objects like sounds or videos) in fields of OLE object type. This type of data is considered a Binary Large Object Bitmap (BLOB).
Of course when processing images, several types of picture formats are available. The most commonly used ones include JPEG, GIF and BMP. Among them, JPEG has been widely accepted by web designers because it requires a small amount of data storage (in other words, the data size of JPEGs is smaller than that of BMPs).
Of course, Delphi has methods for handling BMP, GIF and JPEG graphics formats. The remainder of this chapter will deal with the JPEG file format.
Storing pictures in Access (Storing pictures in Access)
Before discussing how to display images from a database table in a Delphi form, we need to add some graphical data to the database.
Run Access and open the aboutdelphi.mdb library. Open the Applications table (it should have one row of data) and select the Picture field.
Follow these steps to add an image:
1. Select Insert | Object... An Insert Object dialog box will be displayed.