DCEF3:如何获取屏幕截图 [英] DCEF3: How to get a screenshot
问题描述
如何在DCEF3中获取浏览器的屏幕截图?
How to get screenshot of browser in DCEF3?
我在没有VCL的情况下创建了这样的浏览器. TakePicture
方法仅在
I create browser like this without VCL. The TakePicture
method will only work if
- 不使用调试器
-
如果使用ShowWindow
- No debugger is used
If ShowWindow is used
var
info: TCefWindowInfo;
Settings: TCefBrowserSettings;
begin
FillChar(info, SizeOf(info), 0);
info.width := width;
info.height := height;
FillChar(Settings, SizeOf(TCefBrowserSettings), 0);
Settings.Size := SizeOf(TCefBrowserSettings);
GetSettings(Settings);
CefBrowserHostCreateBrowser(@info, FHandler, FDefaultUrl, @settings, nil);
end;
procedure TakePicture(const Browser: ICefBrowser; Height, Width: Integer);
var
DC: HDC;
Bmp : TBitmap;
Handle : HWND;
Rect : trect;
BarHeight : integer;
BarLeft : integer;
begin
Bmp := TBitmap.Create;
Bmp.PixelFormat := pf32bit;
Handle := Browser.Host.WindowHandle;
ShowWindow(handle, SW_RESTORE); // will work only if this is used otherwise black image!
BarLeft := GetSystemMetrics(SM_CXFRAME);
BarHeight := GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME);
GetWindowRect(Handle, Rect);
DC := GetDC(Handle);
Bmp.Width := Rect.Right - Rect.Left;
Bmp.Height := (Rect.Bottom - Rect.Top);
BitBlt(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, DC, -BarLeft, -BarHeight, SRCCOPY);
ReleaseDC(Handle, DC);
Bmp.SaveToFile('c:\test.bmp');
Bmp.Free;
end;
推荐答案
这基本上是屏幕外渲染.在DCEF3的demos文件夹中,您将找到屏幕外"项目.您要查找的代码在 TChromiumOSR
的 OnPaint
事件中.它呈现为 TBitmap32 ,但是任何位图都可以使用.请注意,它已被优化为仅绘制所谓的脏"区域(自上次绘制以来已发生变化的区域),但是如果要截屏,那不是您想要的.在我检出的存储库中,有一行注释掉了,显示了仅绘制所有内容的幼稚情况:
This is basically off-screen rendering. In the demos folder of DCEF3 you'll find a project 'offscreen'. The code you're looking for is in the OnPaint
event of TChromiumOSR
. It renders to a TBitmap32, but any bitmap could be made to work. Notice that it has been optimized to only paint the so-called "dirty" areas (those that have changed since last painting), but if you're making a screenshot, that's not what you want. In my check-out of the repository there's a line commented out showing the naive case of just painting everything:
SomeBitmap.SetSize(width, height);
Move(buffer^, SomeBitmap32.Bits^, width * height * 4);
我最好的猜测是魔术数字 4
代表4个字节(32位).
It's my best guess that the magic number 4
represents 4 bytes (32-bits).
我热烈推荐使用Graphics32,但是您必须使用常规的TBitmap,我将由您自己决定如何将位数组转换为像素.变暖可能会慢很多.
I warmly recommend using Graphics32 but it you have to use a regular TBitmap, I'll leave it up to you to work out how to turn the array of bits into pixels. Be warmed it will probably be a lot slower.
这篇关于DCEF3:如何获取屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!