【问题标题】:retrieve image saved on database [duplicate]检索保存在数据库中的图像[重复]
【发布时间】:2013-02-06 10:28:37
【问题描述】:

我正在使用此代码将图像加载到我的 Timage 中:

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

然后我使用此代码存储到我的 ms 访问数据库中:

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格式

【问题讨论】:

标签: delphi ms-access delphi-xe3


【解决方案1】:

至于 TPicture 无法决定它必须为加载创建哪种 TGraphic,因为流没有像文件名这样的扩展名,您必须决定它并分配 Graphic。
在这种情况下,将 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;

以下单元能够在 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.

【讨论】:

  • TPicture is not able to decide which kind of TGraphic ... since a stream does not have an extension - 至少在 Delphi XE2 中(2013 年推出)你可以查看 VCL 的 TPicture.ReadDataTPicture.WriteData - 你会看到实际图片数据的前缀是实现类名的UTF-8 ShortString,所以只要是同一个应用程序,这甚至比文件扩展名更好。当然,实际使用它需要额外的样板(ComponentToStream DFM 加载/存储序列)
猜你喜欢
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-16
相关资源
最近更新 更多