public async Task CaptureFullPageScreenshot(string url, string outputPath) { try { await new BrowserFetcher().DownloadAsync(BrowserTag.Latest);
using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))
using (var page = await browser.NewPageAsync())
{
await page.GoToAsync(url, WaitUntilNavigation.Networkidle2);
await page.SetViewportAsync(new ViewPortOptions { Width = 1349});
int totalHeight = Convert.ToInt32(await page.EvaluateExpressionAsync("document.body.scrollHeight"));
await page.SetViewportAsync(new ViewPortOptions { Width = 1349, Height = totalHeight });
await page.ScreenshotAsync(outputPath);
}
}
catch (Exception ex)
{
_logger.LogError($"Error while taking screenshot of '{url}' getting exception {ex.Message}");
}
finally {
_logger.LogError($"screenshot captured successfully and stored at '{outputPath}'");
}
}
Above is working code, just want optimize image without loosing quality & Size.
I suggest using the WebP format, which reduces the size of the file without sacrificing image quality.
Test Result
Check the Images online: https://squoosh.app/editor
WebP format
Original format
My test URL: