firemonkeydatasnap

FMX datasnap send a bitmap as a JPG


in Firemonkey, im currently sending a tbitmap as a stream to a datasnap server - but need to reduce the size and possibly lower the quality prior to sending. I would like to do on-the-fly conversion from the TBitmap to a TJpgImage format as its saved into the TMemoryStream. if possible - The solution should include a variable for setting the compressionQuality

var
  ms:TMemmoryStream;
begin
  ms:=TMemmoryStream.create;

  bmpSend.SaveToStream(msSendPic); //somehow convert bmp to jpg on-the-fly

  sz := msSendPic.size;
  msSendPic.Position :=0;
  svr.setPic(s, sz, msSendPic);
end;

Thanks in advance


Solution

  • Use TCodecManager and a TBitmapSurface (unit FMX.surfaces)

    var Surf:TBitmapSurFace  := TBitmapSurface.Create;
    try
      Surf.Assign(bmpSend);
      // use the codec to save Surface to stream
      TBitmapCodecManager.SaveToStream(ms,Surf,'.jpg');
    finally
       Surf.Free;
    end;