/// <summary>/// 保存为图片/// </summary>/// <param name="htmlContent"></param>/// <param name="outputPath"></param>/// <returns></returns>staticasyncTaskGenerateImageFromHtml(string htmlContent,string outputPath){// Launch headless Chrome browserawaitnewBrowserFetcher().DownloadAsync();var browser =await Puppeteer.LaunchAsync(newLaunchOptions{ Headless =true});// Create a new pagevar page =await browser.NewPageAsync();// Set the HTML contentawait page.SetContentAsync(htmlContent);// Generate screenshot of the pageawait page.ScreenshotAsync(outputPath);// Close the browserawait browser.CloseAsync();
Console.WriteLine($"Screenshot saved to: {outputPath}");}
/// <summary>/// 返回字节数组/// </summary>/// <param name="htmlContent"></param>/// <returns></returns>staticasyncTask<byte[]>GenerateImageBytesFromHtml(string htmlContent){// Launch headless Chrome browserawaitnewBrowserFetcher().DownloadAsync();var browser =await Puppeteer.LaunchAsync(newLaunchOptions{ Headless =true});// Create a new pagevar page =await browser.NewPageAsync();// Set the HTML contentawait page.SetContentAsync(htmlContent);// Generate screenshot of the page as bytesvar imageBytes =await page.ScreenshotDataAsync();// Close the browserawait browser.CloseAsync();return imageBytes;}