在 Universal Apps 中捕获当前网站的屏幕截图
Capture screenshot of current web site in Universal Apps
这是我如何捕获当前网页的屏幕截图,但有时它只捕获 webview 的可见区域(用户实际看到的区域)。
但我不确定出了什么问题。
我认为 var heightString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" })
完成得太晚了 - 但程序应该在 if (!int.TryParse(heightString, out height))
中等待完成...我错了吗?
private async Task CaptureWebView()
{
int width;
int height;
var originalWidth = WebView.Width;
var originalHeight = WebView.Height;
// ask the content its width
var widthString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollWidth.toString()" });
// ask the content its height
var heightString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });
if (!int.TryParse(widthString, out width))
{
throw new Exception("Unable to get page width");
}
if (!int.TryParse(heightString, out height))
{
throw new Exception("Unable to get page height");
}
// resize the webview to the content
WebView.Width = width;
WebView.Height = height;
await DoCapture("captured.png");
WebView.Width = originalWidth;
WebView.Height = originalHeight;
Painter.Width = width;
Painter.Height = height;
var i = await LoadCaptured("captured.png");
Painter.Fill = i;
}
测试库on Github
最后问题本身出在 WebView.CapturePreviewToStreamAsync()
方法中。我用它来捕捉完整的网络屏幕截图,但效果不是很好。但后来我发现 WebViewBrush
并意识到这是正确的方法。
所以这是最终代码片段或 GitHub
private async Task CaptureWebView()
{
int width;
int height;
var originalWidth = WebView.ActualWidth;
var originalHeight = WebView.ActualHeight;
var widthString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollWidth.toString()" });
var heightString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });
if (!int.TryParse(widthString, out width))
{
throw new Exception("Unable to get page width");
}
if (!int.TryParse(heightString, out height))
{
throw new Exception("Unable to get page height");
}
// resize the webview to the content
WebView.Width = width;
WebView.Height = height;
var brush = new WebViewBrush();
brush.SetSource(WebView);
Painter.Width = width;
Painter.Height = height;
Painter.Fill = brush;
}
这是我如何捕获当前网页的屏幕截图,但有时它只捕获 webview 的可见区域(用户实际看到的区域)。
但我不确定出了什么问题。
我认为 var heightString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" })
完成得太晚了 - 但程序应该在 if (!int.TryParse(heightString, out height))
中等待完成...我错了吗?
private async Task CaptureWebView()
{
int width;
int height;
var originalWidth = WebView.Width;
var originalHeight = WebView.Height;
// ask the content its width
var widthString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollWidth.toString()" });
// ask the content its height
var heightString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });
if (!int.TryParse(widthString, out width))
{
throw new Exception("Unable to get page width");
}
if (!int.TryParse(heightString, out height))
{
throw new Exception("Unable to get page height");
}
// resize the webview to the content
WebView.Width = width;
WebView.Height = height;
await DoCapture("captured.png");
WebView.Width = originalWidth;
WebView.Height = originalHeight;
Painter.Width = width;
Painter.Height = height;
var i = await LoadCaptured("captured.png");
Painter.Fill = i;
}
测试库on Github
最后问题本身出在 WebView.CapturePreviewToStreamAsync()
方法中。我用它来捕捉完整的网络屏幕截图,但效果不是很好。但后来我发现 WebViewBrush
并意识到这是正确的方法。
所以这是最终代码片段或 GitHub
private async Task CaptureWebView()
{
int width;
int height;
var originalWidth = WebView.ActualWidth;
var originalHeight = WebView.ActualHeight;
var widthString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollWidth.toString()" });
var heightString = await WebView.InvokeScriptAsync("eval", new[] { "document.body.scrollHeight.toString()" });
if (!int.TryParse(widthString, out width))
{
throw new Exception("Unable to get page width");
}
if (!int.TryParse(heightString, out height))
{
throw new Exception("Unable to get page height");
}
// resize the webview to the content
WebView.Width = width;
WebView.Height = height;
var brush = new WebViewBrush();
brush.SetSource(WebView);
Painter.Width = width;
Painter.Height = height;
Painter.Fill = brush;
}