問題は、幅と高さが WebView の作成中に設定され、ページが読み込まれた後に変更するオプションが表示されないことです (実際のサイズが判明しました)。それを回避するには、2 つの 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;
}
もっと良い方法があることを願っています...