下面是Firemonkey上捕获WebBrowser画布的代码段。它在Windows中完美运行,但是在Mac OS中,我得到的是空白屏幕截图。
TBitmap* TForm1::getScreenshotWebBrowser() {
TBitmap *BitmapBuffer;
TBitmap *bmp_Scrhot = new TBitmap;
TRectF SourceRect(0, 0, WebBrowser1->Width, WebBrowser1->Height);
TRect Rect_bmp(0, 0, WebBrowser1->Width, WebBrowser1->Height);
BitmapBuffer = new TBitmap(SourceRect.Width(), SourceRect.Height());
try {
if (BitmapBuffer->Canvas->BeginScene())
try {
WebBrowser1->PaintTo(BitmapBuffer->Canvas, SourceRect);
}
__finally {
BitmapBuffer->Canvas->EndScene();
}
BitmapBuffer->SaveToFile(GetHomePath() +"/screenshot1.png");
bmp_Scrhot->Width = Rect_bmp.Width();
bmp_Scrhot->Height = Rect_bmp.Height();
bmp_Scrhot->CopyFromBitmap(BitmapBuffer, Rect_bmp, 0, 0);
bmp_Scrhot->SaveToFile(GetHomePath() +"/screenshot2.png");
}
__finally {
delete BitmapBuffer;
}
return bmp_Scrhot;
P.S。通过实验,我发现如果捕获包含WebBrowser画布的整个表单,则屏幕截图并非完全空白。它具有表单控件,例如按钮,分隔符等。但是其中的浏览器画布仍为空白。