0

I have added a CEF control into a WinForm. And I have added an invokeCapture method that is expected to capture the screen shot of the entire page of the CEF. It works fine when first invoking. But errors are encountered since second invoking and more, which the message is "Generated MessageID 100002 doesn't match returned Message Id 100001". How can I capture screen shot more than once?

I have copied the screenshot function code from https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Example/DevTools/DevToolsExtensions.cs to my project and renamed the namespace of it to winformcefdemo.CefSharp.Example.

The variable lastMessageId of the class DevToolsClient, in which class CaptureScreenshot executes ExecuteDevToolsMethodAsync in order to run command "Page.CaptureScreenshot", is private and there are either no getter nor setter to it. It seems to be annoying. The method ExecuteDevToolsMethodAsync would want to compare the message ID of what the method ExecuteDevToolsMethod returns to the automatically increased message ID of the DevToolsClient itself. The DevtoolsClient in the method CaptureScreenShotAsPng is what browser.GetDevToolsClient() returns (in the line 36 of the link above). And I have also checked the implementation of the method GetDevToolsClient. It is also using DevToolsClient devToolsClient = new DevToolsClient(browser); in CefSharp.DevToolsExtensions.

        private async void invokeCapture()
        {
            try
            {
                byte[] result = await winformcefdemo.CefSharp.Example.DevTools.DevToolsExtensions.CaptureScreenShotAsPng(browser);
            
                // task.Start();
                // byte[] result = task.Result;
                SaveFileDialog dialog = new SaveFileDialog();
                DialogResult dresult = dialog.ShowDialog();
                if (dresult == DialogResult.OK)
                {
                    string path = dialog.FileName;
                    try
                    {
                        File.WriteAllBytes(path, result);
                        MessageBox.Show(path + " saved success");
                    } catch (Exception e)
                    {
                        MessageBox.Show(path + "Unknown error occurred when saving to file: " + e.Message);
                    }
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show("Unknown error occurred when capturing: " + ee.Message);
            }
        }

1 Answer 1

0

Solved in Chinese community of CSDN

Use no DevToolsExtensions. Use PageClient Instead. DevToolsExtensions does have issues that are not solved.

And PageClient should be defined globally. Do not define it in the method.

        # Source: https://bbs.csdn.net/topics/398544662
        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 Picture (*.PNG)|*.PNG";
                DialogResult dresult = dialog.ShowDialog();
                if (dresult == DialogResult.OK)
                {
                    string path = dialog.FileName;
                    try
                    {
                        File.WriteAllBytes(path, result);
                        MessageBox.Show(path + " saved success");
                    } catch (Exception e)
                    {
                        MessageBox.Show(path + "Unknown error occurred when saving to file: " + e.Message);
                    }
                }
            }
        }
4
  • 1
    This issue should have been resolved in version 87.1.132, upgrading to a newer version would be my recommendation.
    – amaitland
    Feb 21, 2021 at 5:29
  • @amaitland Tried the version 87 but the same exception occurred even on the first screenshot 😂. "Generated MessageId 2 doesn't match returned Message Id 1". Both two paragraphs of code have the issue.
    – No.5972
    Mar 5, 2021 at 8:24
  • Your welcome to submit a PR with a unit test that reproduces the problem, github.com/cefsharp/CefSharp/blob/cefsharp/88/CefSharp.Test/… has a unit test you can use as the basis.
    – amaitland
    Mar 5, 2021 at 9:45
  • @amaitland the PR is submitted to the master branch. Just check it out. https://github.com/cefsharp/CefSharp/pull/3427
    – No.5972
    Mar 8, 2021 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.