Delphi實(shí)現(xiàn)在數(shù)據(jù)庫(kù)中存取圖像

字號(hào):

本實(shí)例演示如何在數(shù)據(jù)庫(kù)中存取圖像文件。
    向窗體上添加一個(gè)TListBox組件、一個(gè)TImage組件和一個(gè)TTable組件,設(shè)計(jì)完成的主界面。
    本系統(tǒng)中需要設(shè)計(jì)一個(gè)新的基于Paradox 7的數(shù)據(jù)庫(kù)Image.db,圖2為設(shè)計(jì)完成的Image.db數(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;