问题是在 WebView 创建期间设置了宽度和高度,并且在页面加载后我看不到任何更改它的选项(并且实际大小已知)。为了解决这个问题,我使用了两个 WebView:第一个是获取大小,第二个是截屏:
static bool finishedLoading;
static bool finishedScroll;
static ScrollData scrollData;
static void Main(string[] args)
{
WebCore.Initialize(new WebCoreConfig() { CustomCSS = "::-webkit-scrollbar { visibility: hidden; }" });
var webView1 = WebCore.CreateWebView(800, 600, false);
webView1.LoadURL("http://someurl");
webView1.LoadCompleted += OnFinishLoading;
while (!finishedLoading)
{
Thread.Sleep(100);
WebCore.Update();
}
webView1.RequestScrollData();
webView1.ScrollDataReceived += new ScrollDataReceivedEventHandler(ScrollDataReceived);
while (!finishedScroll)
{
Thread.Sleep(100);
WebCore.Update();
}
var webView2 = WebCore.CreateWebView(scrollData.ContentWidth, scrollData.ContentHeight, false);
finishedLoading = false;
webView2.LoadURL("http://someUrl");
webView2.LoadCompleted += OnFinishLoading;
while (!finishedLoading)
{
Thread.Sleep(100);
WebCore.Update();
}
webView2.Render().SaveToPNG("filePath");
WebCore.Shutdown();
}
static void ScrollDataReceived(object sender, ScrollDataEventArgs e)
{
finishedScroll = true;
scrollData = e.ScrollData;
}
static void OnFinishLoading(object sender, EventArgs e)
{
finishedLoading = true;
}
我希望有更好的方法...