Unable to take a screenshot of the browser. Black background

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Unable to take a screenshot of the browser. Black background

Postby milton » Mon Aug 14, 2023 2:29 am

I'm using cefsharp 86 (At the moment). I need to take a screenshot of the browser even when the window is behind other windows or outside the screen borders. So that screenshots are taken by themselves, even when you are working with other windows. Below I wrote the only function that can take such a screenshot. But the problem is that the browser frame is completely black, no matter what site is loaded in the browser. I have attached a screenshot to the post.

On version cefsharp 57, screenshots were sometimes black, but this did not bother much, because it was possible to automatically take a second screenshot if the first one turned out to be black. And on version 86, the background is always black and this is a big problem. I abandoned version 57 because it is too old.

Is it possible to fix this?

IF YOU NEED THE CODE TO VERIFY IT, I HAVE ATTACHED THE C# CODE BELOW.

Code: Select all
void Example()
{
    IntPtr hwnd = FindWindow(null, "Example.txt - Notepad2");
    CaptureWindow(hwnd);
}

[DllImport("User32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);

[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect);

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

public void CaptureWindow(IntPtr handle)
{
    // Get the size of the window to capture
    Rectangle rect = new Rectangle();
    GetWindowRect(handle, ref rect);

    // GetWindowRect returns Top/Left and Bottom/Right, so fix it
    rect.Width = rect.Width - rect.X;
    rect.Height = rect.Height - rect.Y;

    // Create a bitmap to draw the capture into
    using (Bitmap bitmap = new Bitmap(rect.Width, rect.Height))
    {
        // Use PrintWindow to draw the window into our bitmap
        using (Graphics g = Graphics.FromImage(bitmap))
        {
            IntPtr hdc = g.GetHdc();
            if (!PrintWindow(handle, hdc, 0))
            {
                int error = Marshal.GetLastWin32Error();
                var exception = new System.ComponentModel.Win32Exception(error);
                Debug.WriteLine("ERROR: " + error + ": " + exception.Message);
                // TODO: Throw the exception?
            }
            g.ReleaseHdc(hdc);
        }

        // Save it as a .jpg just to demo this
        bitmap.Save("Example.jpg", ImageFormat.Jpeg);
    }
}


The screenshot itself is done as follows.

Code: Select all
 CaptureWindow(Handle);


When this function is called, a picture file with a screenshot is saved to the project folder, which I attached to the letter.
Attachments
Example.jpg
Example.jpg (45.08 KiB) Viewed 1605 times
milton
Newbie
 
Posts: 2
Joined: Tue Nov 16, 2021 9:47 pm

Return to Support Forum

Who is online

Users browsing this forum: Google [Bot], W3C [Validator] and 208 guests