2012-12-18 48 views
1

如何使用Blob數據類型將圖像存儲在MS-Access數據庫中? 我想使用記錄類型來存儲數據。那麼,如何處理記錄類型中的圖像以保存在數據庫中呢?使用Delphi6將圖像存儲在MS-Access數據庫中

編輯: 我想用Image存儲數據。 我有以下記錄類型:

type 
    TPersonInfoRecType = Record 
    FirstName: string[20]; 
    MiddleName: string[20]; 
    LastName: string[20]; 
    DateOfBirth: TDateTime; 
    Age: integer; 
    Gender: string[8]; 
    Mobile: string[11]; 
    LandLine: string[13]; 
    Adderss1: string[50]; 
    City: string[15]; 
    State: string[20]; 
    Country: string[20]; 
    ZipCode: string[6]; 
    Photo: TBlobField; 
    end; 
+0

你的意思是_record type_,你能顯示你的實際類型聲明嗎? – jachguate

+0

@jachguate,我添加了_RECORDD TYPE_問題。 – Ganesh

+0

我不明白..從哪裏獲得照片參考?請記住,我們不知道您想要做什麼,因此請在您的問題中解釋背景。花一些時間閱讀[如何寫完美的問題](http://tinyurl.com/so-hints) – jachguate

回答

1

一個possebility,這將允許存儲diffent類型的圖像,可以縮短或擴大RegisterClasses列表。

unit LoadSaveImageBlobs; 

// 20120224 by Thomas Wassermann 

interface 
uses Classes,DB,Graphics,Jpeg,PngImage; 

Procedure SavePicture2Blob(Blob: TBlobField; Picture: TPicture); 
Procedure LoadPictureFromBlob(Picture: TPicture; Blob: TBlobField); 
implementation 

Procedure SavePicture2Blob(Blob: TBlobField; Picture: TPicture); 
var 
    ms, ms2: TMemoryStream; 
    theClassName: AnsiString; 
    len: Byte; 
begin 
    ms := TMemoryStream.Create; 
    try 
    Blob.Clear; 
    theClassName := Picture.Graphic.ClassName; 
    len := Length(theClassName); 
    ms.WriteBuffer(len, 1); 
    if len > 0 then 
     ms.WriteBuffer(theClassName[1], len); 
    ms2 := TMemoryStream.Create; 
    try 
     Picture.Graphic.SaveToStream(ms2); 
     ms2.Position := 0; 
     if ms2.Size > 0 then 
     ms.CopyFrom(ms2, ms2.Size); 
    finally 
     ms2.Free; 
    end; 
    Blob.LoadFromStream(ms); 
    finally 
    ms.Free; 
    end; 
end; 

Procedure LoadPictureFromBlob(Picture: TPicture; Blob: TBlobField); 
var 
    ms, ms2: TMemoryStream; 
    len: Byte; 
    theClassName: AnsiString; 
    Graphic: TGraphic; 
    GraphicClass: TGraphicClass; 
begin 
    ms := TMemoryStream.Create; 
    Blob.SaveToStream(ms); 
    ms.Position := 0; 
    try 
    ms.ReadBuffer(len, 1); 
    SetLength(theClassName, len); 
    if len > 0 then 
     ms.ReadBuffer(theClassName[1], len); 
    GraphicClass := TGraphicClass(FindClass(theClassName)); 
    if (GraphicClass <> nil) and (len > 0) then 
    begin 
     Graphic := GraphicClass.Create; 
     ms2 := TMemoryStream.Create; 
     try 
     ms2.CopyFrom(ms, ms.Size - len - 1); 
     ms2.Position := 0; 
     Graphic.LoadFromStream(ms2); 
     finally 
     ms2.Free; 
     end; 
     Picture.Assign(Graphic); 
    end; 
    finally 
    ms.Free; 
    end; 
end; 


initialization 
RegisterClasses([TIcon, TMetafile, TBitmap, TJPEGImage,TPngImage]); 

end. 
+0

我已經在SavePicture2Blob過程中傳遞'PersonInfoRec.Photo'作爲TBlobField,但出現錯誤:訪問衝突地址爲004ACC5A在模塊中。在第二行:'Blob.clear;' – Ganesh

+0

田地是TBlobfield?字段已分配? Dataset.State在[dsEdit,dsInsert]中? – bummi