gpt4 book ai didi

c# - 在 C# 中使用 Cefsharp 的整页屏幕截图

转载 作者:行者123 更新时间:2023-12-04 12:50:36 26 4
gpt4 key购买 nike

我已经下载了 Minimalexample.Offscreen 的例子。这是我用于屏幕截图的代码,但我没有得到整页。图像被裁剪(仅截取可见页面屏幕截图)。

//   c# code
var scriptTask = browser.EvaluateScriptAsync("document.getElementById('lst-ib').value = 'CefSharp Was Here!'");
scriptTask.ContinueWith(t =>
{
Thread.Sleep(500);
var task = browser.ScreenshotAsync();
task.ContinueWith(x =>
{
var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");

Console.WriteLine();
Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);
task.Result.Save(screenshotPath);
task.Result.Dispose();
Console.WriteLine("Screenshot saved. Launching your default image viewer...");
Process.Start(screenshotPath);
Console.WriteLine("Image viewer launched. Press any key to exit.");
}, TaskScheduler.Default);
}).Wait();

如何使用 CefSharp offscreen 或 Cefsharp winforms 获取完整的长页面截图?

最佳答案

经过很长一段时间后,我找到了解决方案。重点是根据页面高度设置webview高度,然后截图。

CefSharp.OffScreen.ChromiumWebBrowser WebView =new CefSharp.OffScreen.ChromiumWebBrowser(siteUrl);

int width = 1280;
int height = 1480;

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


var executedScript = WebView.EvaluateScriptAsync(jsString).Result.Result;

height = Convert.ToInt32(executedScript);

var size = new Size(width, height);

WebView.Size = size;

Thread.Sleep(500);
// Wait for the screenshot to be taken.
var bitmap = WebView.ScreenshotOrNull();
bitmap.Save(@"Test.jpg");

关于c# - 在 C# 中使用 Cefsharp 的整页屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39411950/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com