delphi 将图像保存到数据流,将其放置在TIimage上

kpbwa7wx  于 2023-03-01  发布在  其他
关注(0)|答案(1)|浏览(308)

有人知道我怎么才能得到一张图片并将其存储在Tstream中?
我的目标是在内存上使用它,并在TIimage中显示给用户。
使用 * OleSavePictureFile * 可以将图像保存为任何格式(PNG,BMP,JPG等),但保存在磁盘上。有人知道如何获得IPicture并将其保存到流中,并将其放在TIimage上吗?
我试过这种方法

// Obtaining the IPicture
  var LPicture := DPBiometria.DPSC.ConvertToPicture(pSample) as IPicture;

// This line saves the image correctly, but i dont want this way.
//  OleSavePictureFile(IPictureDisp(LPicture), 'D:\teste.jpg');

// Trying stores it on the stream and put on a bitmap or show on a TImage
  var LStream := TBytesStream.Create;
  var LOleG := TOleGraphic.Create;
  LOleG.Picture := LPicture;
  LOleG.SaveToStream(LStream);

  // When loading it on a TImage, it doesnt shows...
  // When loadgin on a TBitMap or TJPegImage, and trying to
  // save to a file, its saved with 0 bytes.

  // The TImage doesnt shows the image
  SomeTImage.LoadFromFile(LStream);

  // Saved with 0 bytes
  var Lbmp := TBitmap.Create;
  Lbmp.LoadFromStream(LStream);
  Lbmp.SaveToFile('D:\testeStream.bmp');

谢谢你的帮助

dfty9e19

dfty9e191#

在将图像保存到流之后和加载流之前,不能将TBytesStream.Position属性设置回0。
也就是说,如果您只想在TImage中显示IPcture,则不需要将IPicture保存到流中,然后将流加载到TBitmap中。由于TOleGraphic派生自TGraphic,因此您只需将TOleGraphic直接赋给TImage.Picture.Graphic属性,例如:

var LPicture := DPBiometria.DPSC.ConvertToPicture(pSample) as IPicture;

var LOleG := TOleGraphic.Create;
try
  LOleG.Picture := LPicture;
  SomeTImage.Picture.Graphic := LOleG;
finally
  LOleG.Free;
end;

相关问题