DCEF3: How to get a screenshot

不想你离开。 提交于 2021-01-29 04:04:13

问题


How to get screenshot of browser in DCEF3?

I create browser like this without VCL. The TakePicture method will only work if

  • 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;
    

回答1:


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);

It's my best guess that the magic number 4 represents 4 bytes (32-bits).

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.



来源:https://stackoverflow.com/questions/23755232/dcef3-how-to-get-a-screenshot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!