Delphi Firemonkey TControl.MakeScreenshot 可以在线程中工作吗?

Can Delphi Firemonkey TControl.MakeScreenshot work in a thread?

我创建了一个简单的 FireMonkey 屏幕截图,在 Android 和 Windows 上运行良好..:[=​​15=]

procedure Capture;
var
  B : TBitmap;
begin
  try
    B := Layout1.MakeScreenshot;
    try
      B.SaveToFile( ..somefilepath.png );
    finally
      B.Free;
    end;
  except
    on E:Exception do
      ShowMessage( E.Message );
  end;
end;

结束;

当我将它移动到如下线程时,它在 Windows 中工作正常,但在 Android 中,我从调用 MakeScreenshot 中得到异常 'bitmap too big'。在线程中使用 MakeScreenshot 是否需要额外的步骤?

procedure ScreenCaptureUsingThread;
begin
   TThread.CreateAnonymousThread(
   procedure ()
   var
     B : TBitmap;
   begin
     try
       B := Layout1.MakeScreenshot;
       try
         B.SaveToFile( '...somefilepath.png' );
       finally
         B.Free;
       end;
     except
       on E:Exception do
         TThread.Synchronize( nil,
           procedure ()
           begin
             ShowMessage( E.Message );
           end );
        end).Start;
    end;

稍后补充。根据 Sir Rufo 和 Sebastian Z 的建议,这解决了问题并允许使用线程:

procedure Capture;
begin
  TThread.CreateAnonymousThread(
    procedure ()
    var
      S : string;
      B : TBitmap;
    begin
      try
        // Make a file name and path for the screen shot
        S := '...SomePathAndFilename.png';     
        try
          // Take the screenshot
          TThread.Synchronize( nil,
            procedure ()
            begin
              B := Layout1.MakeScreenshot;
              // Show an animation to indicate success
              SomeAnimation.Start;
            end );

          B.SaveToFile( S );
        finally
          B.Free;
        end;
      except
        on E:Exception do
          begin
          TThread.Synchronize( nil,
            procedure ()
            begin
              ShowMessage( E.Message );
            end );
          end;
      end;
    end).Start;
end;

MakeScreenShot 不是线程安全的,因此您不能在线程中安全地使用它。如果这在 Windows 中有效,那么我会说你很幸运。我建议您在线程外截取屏幕截图,只使用线程将屏幕截图保存为 png。绘画应该很快,而编码为 png 需要更多资源。所以你仍然应该从线程中受益匪浅。