Search code examples
delphidelphi-xe5

Load Image from Stream - range check error


I'm trying to send a picture of a DataSnap server to the client via Stream, more trying to open the stream on the client I get the error "range check error" I get this image of a url and need to send to the client via stream or otherwise.

    //SERVER
function TServerMethods1.DownloadImage(xUrlImg : String; out resp : String) : TStream;
var
  HTTP : TIdHttp;
  Stream : TStream;
  Image : TImage;
  stemp : String;
begin
 try
    HTTP := TIdHttp.Create(nil);
    try
      Stream := TMemoryStream.Create;
      try
        HTTP.ReadTimeout := 10000;
        HTTP.ConnectTimeout := 10000;
        HTTP.Get(xUrlImg, Stream);
        Stream.Position := 0;

        Image := TImage.Create(nil);
        Image.BitMap := TBitMap.Create(0, 0);
        Image.BitMap.LoadFromStream(Stream);
        sTEmp := DateTimeToStr(now).Replace('/', '').Replace(':', '') + '.jpg';
        Image.Bitmap.SaveToFile('C:\SISTEMAS\MOBILE\' + stemp);
        //Image.Free;

        Result := TMemoryStream.Create;
        Image.Bitmap.SaveToStream(Result);
        Result.Seek( 0, TSeekOrigin.soBeginning );
        Result.Position := 0;
        Image.Free;
        //Result.CopyFrom(Stream, 0);
        Stream.Free;
      finally
        //img := Stream;
        resp := '';
        //Stream.Free
      end
    finally
      HTTP.Free
    end;
 Except
   ON E: Exception do
     begin
       resp := 'ERRO: ' + E.Message;
     end;
 end;
end;

//client

    function TFPrin.SalvaImg(URL, xNomeImg : String) : Boolean;
var
  Stream : TStream;
  Image : TImage;
  pathImage,
  sTemp : String;
begin
 try
  Stream := TMemoryStream.Create;
  Stream := ClientModule1.ServerMethods1Client.DownloadImage(URL, sTemp);
  Stream.Position := 0;
  if Trim(sTemp) <> '' then
     begin
      //Stream.Free;
      ShowMessage('Erro: ' + sTemp);
      Exit;
     end;

  pathImage := GetDirPlat + xNomeImg;
  Image := TImage.Create(nil);
  //Image.BitMap := TBitMap.Create(0, 0);
  Image.BitMap.LoadFromStream(Stream);  **//ERROR - RANGE CHECK ERROR**
  Image.Bitmap.SaveToFile(pathImage);
  Image.Free;
  //Stream.Free

 Except
  On E: Exception do
    begin
       ShowMessage('Erro gerar imagem: ' + e.Message);
    end;
 end;
end;

Solution

  • Try something more like this:

    //Server
    function TServerMethods1.DownloadImage(xUrlImg : String; out resp : String) : TStream;
    var
      HTTP : TIdHttp;
      Stream : TStream;
      Bmp : TBitmap;
      Fmt: TFormatSettings;
    begin
      Result := nil;
      try
        Stream := TMemoryStream.Create;
        try
          HTTP := TIdHttp.Create(nil);
          try
            HTTP.ReadTimeout := 10000;
            HTTP.ConnectTimeout := 10000;
            HTTP.Get(xUrlImg, Stream);
          finally
            HTTP.Free;
          end;
    
          Bmp := TBitmap.Create(0, 0);
          try
            Stream.Position := 0;
            Bmp.LoadFromStream(Stream);
    
            Fmt := TFormatSettings.Create;
            Fmt.DateSeparator := #0;
            Fmt.TimeSeparator := #0;
            Bmp.SaveToFile('C:\SISTEMAS\MOBILE\' + DateTimeToStr(Now, Fmt) + '.jpg');
    
            Stream.Clear;
            Bmp.SaveToStream(Stream);
            Stream.Position := 0;
          finally
            Bmp.Free;
          end
        except
          Stream.Free;
          raise;
        end;
        Result := Stream;
      except
        on E: Exception do
        begin
          resp := 'ERRO: ' + E.Message;
        end;
      end;
    end;
    

    //client
    function TFPrin.SalvaImg(URL, xNomeImg : String) : Boolean;
    var
      Stream : TStream;
      Bmp : TBitmap;
      sTemp : String;
    begin
      try
        Stream := ClientModule1.ServerMethods1Client.DownloadImage(URL, sTemp);
        if Stream = nil then
        begin
          ShowMessage('Erro: ' + sTemp);
          Exit;
        end;
        try
          Bmp := TBitmap.Create(0, 0);
          try
            Bmp.LoadFromStream(Stream);
            Bmp.SaveToFile(GetDirPlat + xNomeImg);
          finally
            Bmp.Free;
          end;
        finally
          Stream.Free;
        end;
      except
        on E: Exception do
        begin
          ShowMessage('Erro gerar imagem: ' + e.Message);
        end;
      end;
    end;