我已经下载了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获得完整的长页截图?
答案 0 :(得分:3)
经过很长一段时间,我找到了解决方案。重点是根据页面高度设置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");
答案 1 :(得分:1)
可能是因为您只等待半秒钟才能加载页面。了解加载Google Chrome并花费多少时间。三秒钟添加此
Thread.Sleep(3000);