先安装PuppeteerSharp的库
然后调用如下方法
private async Task ScreenShotAsync(string url)
{
//using var browserFetcher = new BrowserFetcher();
//await browserFetcher.DownloadAsync();
await using var browser = await Puppeteer.LaunchAsync(
new LaunchOptions { Headless = true });
await using var page = await browser.NewPageAsync();
await page.GoToAsync(url);
await page.WaitForTimeoutAsync(1000);
var viewportSize = await page.EvaluateExpressionAsync<dynamic>("({ width: window.innerWidth, height: window.innerHeight })");
await page.ScreenshotAsync("test.png", new ScreenshotOptions()
{
Clip = new Clip
{
X = 0,
Y = 0,
Width = viewportSize.width,
Height = viewportSize.height
}
});
}
指定截图当前页内容,不滚动
如果想要截图所有的界面:
await page.ScreenshotAsync("test.png", new ScreenshotOptions() { FullPage=true});