检索保存在数据库中的图像 [英] retrieve image saved on database

查看:146
本文介绍了检索保存在数据库中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using this code to load images into my Timage:

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

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

Then I'm using this code to store into my ms access database:

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

But now I want to display these saved images on a Timage, can anyone help me? The images are .jpeg format

推荐答案

只要TPicture无法决定哪种类型的TGraphic要创建加载,因为流不具有文件名的扩展名,您必须确定它,并分配图形。

在这种情况下,TJPEGImage到图片。

As far as TPicture is not able to decide which kind of TGraphic it has to create for loading, since a stream does not have an extension like a filename you have to decide it, and assign the Graphic.
In this case a TJPEGImage to the Picture.

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中存储差异的图形格式。
存储与简单存储图像数据不兼容,因为关于图形格式的信息也被存储,以便能够创建所需的加载类。

The following unit is able to store diffent graphicformats in blobfields. Storage is not compatible to simple storing of image data because information about then graphicformat is stored too, to give the ability to create the needed class for loading.

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.

这篇关于检索保存在数据库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆