http://stackoverflow.com/questions/23755232/dcef3-how-to-get-a-screenshot

DCEF3: How to get a screenshot

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;