检索储存在数据库中的图像[复制](retrieve image saved on database

2019-07-19 08:49发布

这个问题已经在这里有一个答案:

  • 如何使用TADOQuery组件只插入图像到数据库 2个回答
  • 使用的Delphi6在MS-Access数据库存储图像 1个回答

我使用这个代码将图像加载到我的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格式

Answer 1:

至于TPicture不能决定它必须创建装载何种TGraphic的,因为流没有像你必须决定它的文件名的扩展,并指定图形。
在这种情况下,TJPEGImage的图片。

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;

下面的单元能够diffent graphicformats存储blobfields。 因为担心再graphicformat信息存储也给创建所需的类装载能力存储不是图像数据的存储简单兼容。

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.


文章来源: retrieve image saved on database [duplicate]