Delphi Firemonkey TControl.MakeScreenshot可以在线程中工作吗? [英] Can Delphi Firemonkey TControl.MakeScreenshot work in a thread?

查看:152
本文介绍了Delphi Firemonkey TControl.MakeScreenshot可以在线程中工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've created a simple FireMonkey screenshot capture that works fine on Android and 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;

end;

当我将其移动到如下所示的线程中时,它在Windows中工作正常,但在Android中,从对MakeScreenshot的调用中得到了异常位图太大".在线程中使用MakeScreenshot是否需要其他步骤?

when I moved it to a thread as below, it works fine in Windows but in Android I get the exception 'bitmap too big' from the call to MakeScreenshot. Are there additional steps needed to employ MakeScreenshot in a thread?

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爵士的建议,这解决了问题并允许使用线程:

Later addition. Based on suggestions by Sir Rufo and Sebastian Z, this solved the problem and allowed use of a thread:

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需要更多的资源.因此,您仍然应该从线程中受益匪浅.

MakeScreenShot is not thread safe, so you cannot safely use it in a thread. If this works in Windows then I'd say you were just lucky. I'd suggest that you take the screenshot outside of the thread and only use the thread for saving the screenshot to png. Painting should be fast while encoding to png needs a lot more resources. So you should still benefit pretty much from the thread.

这篇关于Delphi Firemonkey TControl.MakeScreenshot可以在线程中工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆