我创建了一个简单的 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/