TAdvWebBrowser screenshot issue

Hi,
Since I switched from the TWebBrowser component to TAdvWebBroser, I'm having problems taking screenshots.

As you can see with this image on the screen in my application my TAdvWebBrowser is displayed correctly, but when I make a screenshot (to clipboard or file) the displayed page becomes white.

Any idea where this might be coming from?

Thx.

What are you using to take a screenshot? Note that TAdvWebBrowser is based on Edge Chromium and has a separate CaptureScreenShot method

I'm using this :

Graphics::TBitmap * Bitmap = new Graphics::TBitmap;
Bitmap->Height = ActiveMDIChild->Height;
Bitmap->Width = ActiveMDIChild->Width;
HDC ScreenSrc = GetWindowDC(ActiveMDIChild->Handle);
StretchBlt(Bitmap->Canvas->Handle, 0, 0, ActiveMDIChild->Width, ActiveMDIChild->Height, ScreenSrc, 0, 0, ActiveMDIChild->Width, ActiveMDIChild->Height, SRCCOPY);

This is how my application is structured

*And i want to screenshot the entire TForm not only the TAdvWebBrowser

It doesn't seem possible, the control is based on a separate ICoreWebView2 interface create is a separate window integrated in VCL which attaches the browser child window to the view. Unfortunately it's unclear right now how to paint the browser in an offscreen bitmap. Microsoft did not expose APIs or methods to do this.

ok :confused:

Now if I want to capture my TAdvWebBrowser only I call the CaptureScreenShot function, but after that can I retrieve the TAdvBitmap directly?

I saw that I could treat this TAdvBitmap extactement like the current Graphics::TBitmap simply by using the Bitmap property. But I would like this TAdvBitmap to be accessible directly after the call to the screenshot function without having to go through OnCaptureScreenShot

DoCaptureScreenShot is close to what I'm looking for but it's protected so it can't be used.

Is this possible? Thx.

Hi,

No, the OnCaptureScreenShot is called asynchronously. This is default behaviour of the TAdvWebBrowser ICoreWebView2 implementation.

I assume that when the OnCaptureScreenShot event is triggered the TAdvBitmap *AScreenShot pointer contains my screenshot.

BUT... after saving this TAdvBitmap to a file, the file is empty and nothing is added to the clipboard.

As always, it works in a simple application (TForm + TAdvWebBrowser)

// Save to clipboard
TJPEGImage * pJPEGImage = new TJPEGImage();
try
{
  Word MyFormat;
  THandle AData;
  HPALETTE APalette;

  pJPEGImage->Assign(AScreenShot->Bitmap);
  pJPEGImage->SaveToClipboardFormat(MyFormat, AData, APalette);
  Clipboard()->SetAsHandle(MyFormat, AData);
}
__finally
{
  delete pJPEGImage;
}

When are you accessing AScreenShot? Please note that only within the event itself, AScreenShot is valid.

Directly in the event so when it's valid

Can you try saving it to a PNG instead

Replacing TJPEGImage with TPngImage gives me the following error (translated from French to English)

Some operations could not be performed because the system has run out of resources. Close a few windows and try again

uses
  pngimage;

procedure TForm89.AdvWebBrowser1CaptureScreenShot(Sender: TObject;
  AScreenShot: TAdvBitmap);
var
  p: TPNGImage;
begin
  p := TPngImage.Create;
  try
    p.Assign(AScreenShot);
    p.SaveToFile('test.png');
  finally
    p.Free;
  end;
end;

We have tried with this code and everything is working as expected.

I'm not familiar with Delphi, does it look like the same as my C++ code ?

TPngImage * pPNGImage = new TPngImage();
try
{
  pPNGImage->Assign(AScreenShot->Bitmap);
  pPNGImage->SaveToFile(strFileName);
}
__finally
{
  delete pPNGImage;
}

Otherwise moving to this for the clipboard works well

Word MyFormat;
THandle AData;
HPALETTE APalette;

AScreenShot->SaveToClipboardFormat(MyFormat, AData, APalette);
Clipboard()->SetAsHandle(MyFormat, AData);

Just use AScreenShot, not AScreenShot->Bitmap

Yep works fine :+1:

Thx Pieter

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.