C# CEF uses built-in devtools to take screenshots of the entire webpage

21.01.18 update: The big guy of the CSDN forum gave a solution , you can call the screenshot multiple times. Also posted here for reference.
Here is changed to use global variables PageClientto temporarily store page objects. Because if you put this definition statement in the method, it will still report the error "Generated MessageID 100002 doesn't match returned Message Id 100001", so it must be placed in a global variable.

        CefSharp.DevTools.Page.PageClient pageClien= null;
        private async void invokeCapture()
        {
    
    
           if(pageClien==null)
            {
    
    
                pageClien =  webBrowser.GetBrowser().GetDevToolsClient().Page;
            }

            var result = await pageClien.CaptureScreenshotAsync();
            
            if (result.Data != null)
            {
    
    

                MemoryStream ms = new MemoryStream(result.Data);
                ms.Write(result.Data, 0, result.Data.Length);

                SaveFileDialog dialog = new SaveFileDialog();
                dialog.Filter = "PNG图片 (*.PNG)|*.PNG";
                DialogResult dresult = dialog.ShowDialog();
                if (dresult == DialogResult.OK)
                {
    
    
                    string path = dialog.FileName;
                    try
                    {
    
    
                        File.WriteAllBytes(path, result.Data);
                        MessageBox.Show(path + "保存成功。");
                    }
                    catch (Exception e)
                    {
    
    
                        MessageBox.Show(path + "保存失败!错误信息:" + e.Message);
                    }
                }
            }
        }

One advantage of this is that there is no need to call the Win32 low-level API. You can take a screenshot by directly taking the CEF component method, and you don’t need to dedicate the entire OffScreencomponent. After all , it needs to be copied and inherited, and it also takes up memory. And even if the CEF window exceeds the screen, or is blocked by other windows, or even uses special means to enlarge the window to a resolution larger than the screen, this method can also be intercepted.
But this method can only take a screenshot once, not multiple times. You must quit and restart to continue the screenshot. The second screenshot will report the error "Generated MessageID 100002 doesn't match returned Message Id 100001". There is no solution online, including foreign communities. I have already asked this question on StackOverflow. Copy
the code https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/DevTools/DevToolsExtensions.cs on Github , put it in the project, and change the namespace. Then you can directly call the CEF control. The GUI displayed by WinForm is used here. Then write the method in the code to call it.

        private async void invokeCapture()
        {
    
    
            try
            {
    
    
                byte[] result = await CefSharp.Example.DevTools.DevToolsExtensions.CaptureScreenShotAsPng(browser); // browser是CEF控件实例
                SaveFileDialog dialog = new SaveFileDialog();
                dialog.Filter = "PNG图片 (*.PNG)|*.PNG";
                DialogResult dresult = dialog.ShowDialog();
                if (dresult == DialogResult.OK)
                {
    
    
                    string path = dialog.FileName;
                    try
                    {
    
    
                        File.WriteAllBytes(path, result);
                        MessageBox.Show(path + "保存成功。");
                        
                    } catch (Exception e)
                    {
    
    
                        MessageBox.Show(path + "保存失败!错误信息:" + e.Message);
                    }
                }

            }
            catch (Exception ee)
            {
    
    
                MessageBox.Show("目前暂时只支持截一次图,暂时不支持截更多次数的图片,如果要继续截图得退出程序重开。作者确实没法解决这个问题了,谁有好的想法也欢迎提出来,具体详情请关注https://stackoverflow.com/questions/65334430/message-id-went-wrong-when-using-cef-devtools-executedevtoolsmethodasync-and-pag 。相关技术细节:" + ee.Message, "暂不支持的操作", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

Guess you like

Origin blog.csdn.net/qq_35977139/article/details/111319817