This example shows how to access an image file in a database.
Add a Tlistbox component, a timage component, and a ttable component to the form, and the design-complete main interface is shown in Figure 1.
Figure 1 Main interface
This system needs to design a new database image.db based on Paradox 7, Figure 2 is the design completed image.db database.
Figure 2 The database designed for completion
To facilitate the testing process, the IMAGE.DB database is stored in the path where the instance program resides.
The TableName property of the Ttable component is set to True for the Image.db,active property.
At the beginning of the program, you first determine if there are records in the Image.db database, and if no records exist, then execute the following code to add the "Bird. bmp" File to the Image.db database:
Procedure Tform1.formcreate (Sender:tobject);
Var
Mem:tmemorystream;
Begin
If table1.eof and Table1.bof then
Begin
With Table1 do
Begin
Insert;
Fieldbyname (' Name '). asstring:= ' bird ';
Mem:=tmemorystream.create ();
Mem. LoadFromFile (' Bird. bmp ');
Tblobfield (Fieldbyname (' Data ')). Loadfromstream (MEM);
Post;
End
End
End
Then add "Sample. wav", "leaf. wav" and "lotus" image files to the Image.db database in the same order.
Finally, add the name of the file stored in the IMAGE.DB database to the Tlistbox component of the form by using the following code:
With Table1 do
Begin
A;
While does Eof do
Begin
LISTBOX1.ITEMS.ADD (Fieldbyname (' Name '). asstring);
Next;
End
End
As the program runs, if the user chooses an image file in the Tlistbox component of the form, the program passes the 1th record in the datasheet as the current record through the Ttable component's one, and then loops through the records in the database. If the content of the Name field in a record is the same as the user's choice, then the image information from the data field in the record is read out and the image is displayed on the Timage component on the form. The code is as follows:
Procedure Tform1.listbox1click (Sender:tobject);
Var
Mem:tstream;
Bmp:tbitmap;
Begin
With Table1 do
Begin
A;
While does Eof do
Begin
If Fieldbyname (' Name '). Asstring=listbox1.items[listbox1.itemindex] Then
Break
Next;
End
Bmp:=tbitmap.create;
Mem:=createblobstream (Fieldbyname (' Data '), bmread);
Mem. position:=0;
Bmp. Loadfromstream (MEM);
Self. Image1.Picture.Assign (BMP);
Bmp. Free;
Mem. Free;
End
End
The program code is as follows:
Unit Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, DB, Dbtables,mmsystem, Extctrls;
Type
TForm1 = Class (Tform)
Listbox1:tlistbox;
table1:ttable;
Image1:timage;
Procedure Formcreate (Sender:tobject);
Procedure Listbox1click (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End
Var
Form1:tform1;
Implementation
{$R *.DFM}
Procedure Tform1.formcreate (Sender:tobject);
Var
Mem:tmemorystream;
Begin
If table1.eof and Table1.bof then
Begin
With Table1 do
Begin
Insert;
Fieldbyname (' Name '). asstring:= ' bird ';
Mem:=tmemorystream.create ();
Mem. LoadFromFile (' Bird. bmp ');
Tblobfield (Fieldbyname (' Data ')). Loadfromstream (MEM);
Post;
Insert;
Fieldbyname (' Name '). asstring:= ' sample ';
Mem:=tmemorystream.create ();
Mem. LoadFromFile (' Sample. bmp ');
Tblobfield (Fieldbyname (' Data ')). Loadfromstream (MEM);
Post;
Insert;
Fieldbyname (' Name '). Asstring:= ' leaves ';
Mem:=tmemorystream.create ();
Mem. LoadFromFile (' leaf. bmp ');
Tblobfield (Fieldbyname (' Data ')). Loadfromstream (MEM);
Post;
Insert;
Fieldbyname (' Name '). asstring:= ' Lotus ';
Mem:=tmemorystream.create ();
Mem. LoadFromFile (' Lotus. bmp ');
Tblobfield (Fieldbyname (' Data ')). Loadfromstream (MEM);
Post;
Mem. Free;
End
End
With Table1 do
Begin
A;
While does Eof do
Begin
LISTBOX1.ITEMS.ADD (Fieldbyname (' Name '). asstring);
Next;
End
End
End
Procedure Tform1.listbox1click (Sender:tobject);
Var
Mem:tstream;
Bmp:tbitmap;
Begin
With Table1 do
Begin
A;
While does Eof do
Begin
If Fieldbyname (' Name '). Asstring=listbox1.items[listbox1.itemindex] Then
Break
Next;
End
Bmp:=tbitmap.create;
Mem:=createblobstream (Fieldbyname (' Data '), bmread);
Mem. position:=0;
Bmp. Loadfromstream (MEM);
Self. Image1.Picture.Assign (BMP);
Bmp. Free;
Mem. Free;
End
End
End.
Save the file, and then press F9 to run the program, the initial picture of the program running as shown in Figure 3.
When an item is selected in the Tlistbox component, the corresponding image file is displayed, as shown in Figure 4.
Figure 3 Initial screen of program operation