C# 使用C语言的Cefsharp的完整页面截图#
我已经下载了示例Minimalexample.Offscreen。这是我用于截图的代码,但我没有得到完整的页面。图像被裁剪(仅拍摄可见页面截图)C# 使用C语言的Cefsharp的完整页面截图#,c#,screenshot,cefsharp.offscreen,C#,Screenshot,Cefsharp.offscreen,我已经下载了示例Minimalexample.Offscreen。这是我用于截图的代码,但我没有得到完整的页面。图像被裁剪(仅拍摄可见页面截图) 如何使用CefSharp offscreen或CefSharp winforms获取完整的长页屏幕截图?可能是因为您只需等待半秒钟即可加载页面。看看在谷歌浏览器上加载需要多少时间,并给出多少秒。加上这个三秒钟 Thread.Sleep(3000); 经过很长一段时间,我找到了解决办法。主要是根据页面高度设置webview高度,然后拍摄屏幕截图 Cef
如何使用CefSharp offscreen或CefSharp winforms获取完整的长页屏幕截图?可能是因为您只需等待半秒钟即可加载页面。看看在谷歌浏览器上加载需要多少时间,并给出多少秒。加上这个三秒钟
Thread.Sleep(3000);
经过很长一段时间,我找到了解决办法。主要是根据页面高度设置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");
为什么不使用
async/await
?没有支持的.NET版本具有任务但不具有异步/等待功能(提示)。这将使您的代码更干净,并避免由于错过Dispose
调用而导致的泄漏。由于您的页面可能未完全加载,所以ScreenshotAsync
只捕获了第一个帧,你能给出任何演示代码示例吗?没有任何示例,也没有其他用于屏幕截图的函数只有ScreenshotAsync可用。现在我更新了代码并添加了等待,但仍然存在相同的问题//我还运行了代码var bitmap=browser.ScreenshotOrNull();保存(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),“ScreenPage.png”);//但仍然收到交叉图像
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");