1

I've developed a datasnap rest server application in RAD Studio 10.3.2. In one of my server methods I receive an image from the client app. The image data is a base64 encoded string as a json value. My method is something like this:

function TServerMethods1.getImage(JSONobj: TJSONObject): Boolean;
var
  OutputStream : TMemoryStream;
  InputStream  : TStringStream;
  theImage     : TBitmap;
begin
  var imageStr    :  string := JSONobj.GetValue('Image').Value;
  InputStream     := TStringStream.Create(imageStr);
//InputStream.saveToFile('C:\InputStream.txt');
  OutputStream    := TMemoryStream.Create;
  theImage        := TBitmap.Create;
  try
    InputStream.Position  := 0;
    TNetEncoding.Base64.Decode(InputStream, OutputStream);
  //OutputStream.saveToFile('C:\OutputStream.txt'); 
    OutputStream.Position := 0;
    theImage.LoadFromStream(OutputStream);  // in this line I get an access violation error!
  finally
    theStringStream.Free;
    theMemoryStream.Free;
  end;
  .
  .
  .
end;

When I build the project as a standalone firemonkey app (.exe file) everything works fine but when I build an ISAPI dll and deploy it in IIS I got an access violation error in the line that I added a comment to it. What's wrong? I'm really confused!

P.S.

  1. I saved both InputStream and OutputStream somewhere so that I get sure that I receive the stream and decode it properly and both streams are just fine.

  2. The variable theImage: TBitmap; is an object of FMX.Graphics.TBitmap class, because my stand-alone GUI is a firemonkey application.

4

1 回答 1

1

TBitmapfmx 的类似乎与 ISAPI dll 有问题。所以FMX.Graphics我没有使用,而是使用TJPEGImage了 vcl 类。为了实现这一点,我添加Vcl.Imaging.jpegusesServerMethodsUnit 部分。然后我将以前的功能更改为:

function TServerMethods1.getImage(JSONobj: TJSONObject): Boolean;
var
  OutputStream : TMemoryStream;
  InputStream  : TStringStream;
  theImage     : TJPEGImage;   // using TJPEGImage instead of FMX.Graphics.TBitmap
begin
  var imageStr    :  string := JSONobj.GetValue('Image').Value;
  InputStream     := TStringStream.Create(imageStr);
  OutputStream    := TMemoryStream.Create;
  theImage        := TJPEGImage.Create;
  try
    InputStream.Position  := 0;
    TNetEncoding.Base64.Decode(InputStream, OutputStream);
    OutputStream.Position := 0;
    theImage.LoadFromStream(OutputStream);  // Now this line works fine!
  finally
    theStringStream.Free;
    theMemoryStream.Free;
  end;
  .
  .
  .
end;

现在我可以从内存流中加载接收到的图像并将其保存为 jpeg 图像文件。

于 2020-07-21T17:57:02.867 回答