This example demonstrates how to access image files in the database.
Add a TListBox component, a TImage component and a TTable component to the form. The designed main interface is shown in Figure 1.
Figure 1 Main interface
In this system, a new database Image.db based on Paradox 7 needs to be designed. Figure 2 shows the completed Image.db database.
Figure 2 The designed database
In order to facilitate testing the program, the Image.db database is stored in the path where the example program is located.
Set the TableName property of the TTable component to Image.db and the Active property to True.
At the beginning of the program running, it will first determine whether there is a record in the Image.db database. If no record exists, 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 the "sample.wav", "leaf.wav" and "lotus" image files to the Image.db database sequentially in the same way.
Finally, add the file name stored in the Image.db database to the TListBox component of the form through the following code:
with Table1 do
begin
First;
while not Eof do
begin
ListBox1.Items.Add(FieldByName('Name').AsString);
Next;
end;
end;
During the running of the program, if the user selects an image file in the TListBox component of the form, the program will use the First method of the TTable component to set the first record in the data table as the current record, and then traverse the database through a loop. Record. If the content of the Name field in a certain record is the same as the user's selection, then the image information in 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
First;
while not 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:='leaf';
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
First;
while not 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
First;
while not 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 the F9 key to run the program. The initial screen of the program is shown in Figure 3.
After selecting an item in the TListBox component, the corresponding image file will be displayed, as shown in Figure 4.
Figure 3 Initial screen of program running