如何在 DCEF3 中获取浏览器的屏幕截图?
我在没有 VCL 的情况下创建这样的浏览器。该TakePicture
方法仅在以下情况下有效
- 没有使用调试器
如果使用 ShowWindow
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;