Search code examples
delphicomdelphi-10.4-sydney

Save an IPicture to Stream, putting it on a TImage


Someone knows how can i get a IPicture and stores it in a TStream?

My goals is work with that on memory, and shows to the user in a TImage.

Using the OleSavePictureFile the image can be saved to any format (PNG, BMP, JPG etc), but, saving on disk. Someone knows how get the IPicture and save it to the stream and put it on a TImage?

I've tried by this way

// 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');

Thanks for any help


Solution

  • You are not setting the TBytesStream.Position property back to 0 after saving the image to the stream and before loading the stream.

    That being said, if all you want to do is display the IPcture in a TImage, you don't need to save the IPicture to a stream and then load the stream into a TBitmap. Since TOleGraphic derives from TGraphic, you can simply assign the TOleGraphic directly to the TImage.Picture.Graphic property, eg:

    var LPicture := DPBiometria.DPSC.ConvertToPicture(pSample) as IPicture;
    
    var LOleG := TOleGraphic.Create;
    try
      LOleG.Picture := LPicture;
      SomeTImage.Picture.Graphic := LOleG;
    finally
      LOleG.Free;
    end;