2013-02-06 45 views
2

我使用這個代碼將圖像加載到我的TImage:檢索圖像保存在數據庫

begin 
if OpenPictureDialog1.Execute(Self.Handle) then 
Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName); 
end; 

然後,我用這個代碼存儲到我的MS Access數據庫:

var 
AStream : TMemoryStream; 
begin 
Adotable1.Append; 

AStream := TMemoryStream.Create; 
try 
Image1.Picture.Graphic.SaveToStream(AStream); 
AStream.Position := 0; 
if Adotable1.Active then 
begin 
    TBlobField(Adotable1.FieldByName('Termograma')).LoadFromStream(AStream); 
    end; 
finally 
AStream.Free; 
end; 
adotable1.Post; 

但現在我想在Timage上顯示這些保存的圖像,任何人都可以幫助我嗎? 圖像是.jpeg格式

+0

使用'TBLobStream'和'TPicture.LoadFromStream' /// http://stackoverflow.com/search?q=%5Bdelphi%5D+blob+picture –

+0

@ Arioch'The,你能告訴我一些碼? – Ammadeux

回答

3

只要TPicture不能決定它需要創建加載哪種類型的TGraphic,因爲一個流沒有像文件名一樣的擴展名,你必須決定它,並分配圖形。
在這種情況下TJPEG圖像的圖片。

var 
JPG:TJPEGImage; 
ms:TMemoryStream; 
begin 
    JPG:=TJPEGImage.Create; 
    ms:=TMemoryStream.Create; 
    try 
    TBlobField(AdoTable1.FieldByName('Termograma')).SaveToStream(ms); 
    ms.Position := 0; 
    JPG.LoadFromStream(ms); 
    Image2.Picture.Assign(JPG); 
    finally 
     JPG.Free; 
     ms.Free; 
    end; 
end; 

以下單元能夠在blobfields中存儲不同的圖形格式。 存儲與圖像數據的簡單存儲不兼容,因爲有關圖形格式的信息也被存儲起來,從而無法創建所需的加載類。

unit LoadSaveImageBlobs; 

// 20120224 by Thomas Wassermann 
// Adapt. RegisterClasses and uses for your requirements 
// based on an Idea of Emiliano Sos 

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 
// you might register others if wished 
RegisterClasses([TIcon, TMetafile, TBitmap, TJPEGImage,TPngImage]); 

end.