欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Delphi實(shí)現(xiàn)在數(shù)據(jù)庫(kù)中存取圖像的圖文演示無錯(cuò)

 更新時(shí)間:2008年01月01日 16:07:35   作者:  
最近打算學(xué)習(xí)Delphi實(shí)現(xiàn)在數(shù)據(jù)庫(kù)中存取圖像,網(wǎng)上的好多Delphi實(shí)現(xiàn)在數(shù)據(jù)庫(kù)中存取圖像都是錯(cuò)誤的,所以我把圖片給弄好了。

本實(shí)例演示如何在數(shù)據(jù)庫(kù)中存取圖像文件。

向窗體上添加一個(gè)TListBox組件、一個(gè)TImage組件和一個(gè)TTable組件,設(shè)計(jì)完成的主界面如圖1所示。

圖1 主界面

本系統(tǒng)中需要設(shè)計(jì)一個(gè)新的基于Paradox 7的數(shù)據(jù)庫(kù)Image.db,圖2為設(shè)計(jì)完成的Image.db數(shù)據(jù)庫(kù)。

圖2 設(shè)計(jì)完成的數(shù)據(jù)庫(kù)

為了方便測(cè)試程序,Image.db數(shù)據(jù)庫(kù)存儲(chǔ)在實(shí)例程序所在的路徑下。

設(shè)置TTable組件的TableName屬性為Image.db,Active屬性為True。

在程序運(yùn)行初期,首先會(huì)判斷Image.db數(shù)據(jù)庫(kù)中是否存在記錄,如果沒有記錄存在,那么就執(zhí)行以下代碼向Image.db數(shù)據(jù)庫(kù)中添加“鳥.bmp”文件:

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:='鳥';
mem:=TMemoryStream.Create();
mem.LoadFromFile('鳥.bmp');
TBlobField(FieldByName('Data')).LoadFromStream(mem);
Post;
end;
end;
end;
然后按照相同的方式順序向Image.db數(shù)據(jù)庫(kù)中添加“樣品.wav”、“葉子.wav”和“荷花”圖像文件。

最后通過下面的代碼把Image.db數(shù)據(jù)庫(kù)中存儲(chǔ)的文件名字添加到窗體的TListBox組件中:

with Table1 do
begin
First;
while not Eof do
begin
ListBox1.Items.Add(FieldByName('Name').AsString);
Next;
end;
end;
在程序運(yùn)行過程中,如果用戶在窗體的TListBox組件中選擇了圖像文件,程序會(huì)通過TTable組件的First方法把數(shù)據(jù)表中的第1條記錄作為當(dāng)前記錄,然后通過一個(gè)循環(huán)來遍歷數(shù)據(jù)庫(kù)中的記錄。如果某條記錄中Name字段的內(nèi)容和用戶的選擇相同,那么就把該記錄中Data字段中的圖像信息讀取出來,并且把圖像顯示在窗體上的TImage組件上。代碼如下:

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;
程序代碼如下:

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:='鳥';
mem:=TMemoryStream.Create();
mem.LoadFromFile('鳥.bmp');
TBlobField(FieldByName('Data')).LoadFromStream(mem);
Post;
Insert;
FieldByName('Name').AsString:='樣品';
mem:=TMemoryStream.Create();
mem.LoadFromFile('樣品.bmp');
TBlobField(FieldByName('Data')).LoadFromStream(mem);
Post;
Insert;
FieldByName('Name').AsString:='葉子';
mem:=TMemoryStream.Create();
mem.LoadFromFile('葉子.bmp');
TBlobField(FieldByName('Data')).LoadFromStream(mem);
Post;
Insert;
FieldByName('Name').AsString:='荷花';
mem:=TMemoryStream.Create();
mem.LoadFromFile('荷花.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.
保存文件,然后按F9鍵運(yùn)行程序,程序運(yùn)行的初始畫面如圖3所示。

在TListBox組件中選中一項(xiàng)后,就會(huì)顯示相對(duì)應(yīng)的圖像文件,如圖4所示。

圖3 程序運(yùn)行的初始畫面

相關(guān)文章

最新評(píng)論