winforms Cefsharp的全页截图

esyap4oy  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(422)

我有Cefsharp winforms v.104
我需要保存整个页面的截图。这段代码改变了浏览器的分辨率。我认为这是错误的方法。
您能指定如何正确保存整个页面吗?

int width = 1024;
        int height = 768;

        string jsString = "Math.max(document.body.scrollHeight, " +
                          "document.documentElement.scrollHeight, document.body.offsetHeight, " +
                          "document.documentElement.offsetHeight, document.body.clientHeight, " +
                          "document.documentElement.clientHeight);";

        JavascriptResponse JSresponse = await chromeBrowser.EvaluateScriptAsync(jsString);

        height = Convert.ToInt32(JSresponse.Result);

        var client = chromeBrowser.GetDevToolsClient();
        await client.Emulation.SetDeviceMetricsOverrideAsync(width, height, 1, true);

        await Task.Delay(1000);

        await chromeBrowser.CaptureScreenshotAsync().ContinueWith(t => {
            using (FileStream fs = new FileStream(@"D:\\" + DateTime.Now.Ticks + ".jpg", FileMode.Create, FileAccess.ReadWrite))
            {
                byte[] b = t.Result;
                fs.Write(b, 0, b.Length);
            }
        });
sxissh06

sxissh061#

谢谢amaitland

var contentSize = await chromiumWebBrowser.GetContentSizeAsync();

        var viewPort = new DevTools.Page.Viewport
        {
            Width = contentSize.Width,
            Height = contentSize.Height,
        };

        var data = await chromiumWebBrowser.CaptureScreenshotAsync(viewPort: viewPort, captureBeyondViewport: true);

        var screenshotPath = @"D:\\" + DateTime.Now.Ticks + ".jpg";

        File.WriteAllBytes(screenshotPath, data);

相关问题