delphifiremonkeydatasnapisapirad-studio

Why I get access violation error from TBitmap.LoadFromStream method in datasnap ISAPI dll?


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.


Solution

  • It seems that the TBitmap class of fmx has problems with ISAPI dll. So Instead of using FMX.Graphics I utilized TJPEGImage class of vcl. In order to achieve that I added Vcl.Imaging.jpeg to uses section of ServerMethodsUnit. Then I changed my previous function to this:

    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;
    

    Now I can load the received image from memorystream and save it as a jpeg image file.