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

标签 android multithreading delphi screenshot firemonkey

我创建了一个简单的 FireMonkey 屏幕截图,可以在 Android 和 Windows 上正常运行......:

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 时收到异常“位图太大”。在线程中使用 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;

稍后补充。根据 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 需要更多资源。所以你仍然应该从线程中受益匪浅。

关于android - Delphi Firemonkey TControl.MakeScreenshot 可以在线程中工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34238992/

相关文章:

java - 重写 Application 类并通过静态变量访问它是否安全?

java - JSON API Android解析教程

android - 在没有android浏览器帮助的情况下在android中打开一个url?

java - Java中使用ThreadPools在K个线程(N*M >> K)上处理N个用户的M个任务

ASP.NET MVC 多线程

Delphi 2009 IDE结构 View 折叠功能

java - 不幸的是 MyApp 已停止。我该如何解决这个问题?

java - 由于守护线程,Jmeter 未完成

c# - 如何使用 p/invoke 编码 Delphi 短字符串?

delphi - 是否可以获取组件的方法值(即名称)?