Best Puppeteer-sharp code snippet using PuppeteerSharp.ScreenshotOptions
LottoXClient.cs
Source:LottoXClient.cs
...146 private async Task TakeTopOrderScreenshot(string filepath)147 {148 Page page = await GetPage();149 await page.ScreenshotAsync(filepath,150 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 1000, Height = 400, X = 1000, Y = 350 } });151 }152 private async Task TakeOrdersScreenshot(string filepath)153 {154 Page page = await GetPage();155 await page.ScreenshotAsync(filepath,156 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 983, Height = 1440, X = 1017 } });157 }158 private async Task TakeHeaderScreenshot(string filepath)159 {160 Page page = await GetPage();161 await page.ScreenshotAsync(filepath,162 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 1000, Height = 350, X = 250 } });163 }164 private async Task TakeQuantityColumnScreenshot(string filePath)165 {166 Page page = await GetPage();167 await page.ScreenshotAsync(filePath,168 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 65, Height = 350, X = 477, Y = 400 } });169 }170 private async Task TakePortfolioScreenshot(string filePath)171 {172 Page page = await GetPage();173 await page.ScreenshotAsync(filePath,174 new ScreenshotOptions { Clip = new PuppeteerSharp.Media.Clip { Width = 1000, Height = 1440 } });175 }176 }177}...
SaveWebPage.xaml.cs
Source:SaveWebPage.xaml.cs
...44 var page = await browser.NewPageAsync(); //æå¼ä¸ä¸ªæ°æ ç¾45 await page.GoToAsync(this.tbox_Url.Text); //访é®é¡µé¢4647 //设置æªå¾é项48 PuppeteerSharp.ScreenshotOptions screenshotOptions = new PuppeteerSharp.ScreenshotOptions();49 //screenshotOptions.Clip = new PuppeteerSharp.Media.Clip() { Height = 0, Width = 0, X = 0, Y = 0 };//设置æªåªåºå50 screenshotOptions.FullPage = true; //æ¯å¦æªåæ´ä¸ªé¡µé¢51 screenshotOptions.OmitBackground = false;//æ¯å¦ä½¿ç¨éæèæ¯ï¼èä¸æ¯é»è®¤ç½è²èæ¯52 screenshotOptions.Quality = 100; //æªå¾è´¨é 0-100ï¼pngä¸å¯ç¨ï¼53 screenshotOptions.Type = PuppeteerSharp.ScreenshotType.Jpeg; //æªå¾æ ¼å¼5455 var fileName = Environment.CurrentDirectory + $"\\download\\{await page.GetTitleAsync()}.jpg";5657 if (System.IO.File.Exists(fileName))58 {59 fileName = fileName.Replace(".jpg", $"{DateTime.Now.ToString("ffff")}.jpg");60 }6162 await page.ScreenshotAsync(fileName, screenshotOptions);
...
Form1.cs
Source:Form1.cs
...47 //è·åæåçæ ç¾é¡µ48 var page = (await browser.PagesAsync()).Last();49 var path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"screenshot.jpg");50 //设置æªå¾é项51 ScreenshotOptions screenshotOptions = new ScreenshotOptions();52 //screenshotOptions.Clip = new PuppeteerSharp.Media.Clip() { Height = 0, Width = 0, X = 0, Y = 0 };//设置æªåªåºå53 screenshotOptions.FullPage = true; //æ¯å¦æªåæ´ä¸ªé¡µé¢54 screenshotOptions.OmitBackground = false;//æ¯å¦ä½¿ç¨éæèæ¯ï¼èä¸æ¯é»è®¤ç½è²èæ¯55 screenshotOptions.Quality = 100; //æªå¾è´¨é 0-100ï¼pngä¸å¯ç¨ï¼56 screenshotOptions.Type = ScreenshotType.Jpeg; //æªå¾æ ¼å¼ 57 await page.ScreenshotAsync(path,screenshotOptions);58 MessageBox.Show($"æªå¾å·²ç»ä¿åè³{path}");59 }60 private async void button3_Click(object sender, EventArgs e)61 {62 //è·åæåçæ ç¾é¡µ63 var page = (await browser.PagesAsync()).Last();64 var path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "screenshot.pdf");65 //设置PDFé项...
ScreenshotOptions.cs
Source:ScreenshotOptions.cs
...5using PuppeteerSharp.Media;6namespace PuppeteerSharp7{8 /// <summary>9 /// Options to be used in <see cref="Page.ScreenshotAsync(string, ScreenshotOptions)"/>, <see cref="Page.ScreenshotStreamAsync(ScreenshotOptions)"/> and <see cref="Page.ScreenshotDataAsync(ScreenshotOptions)"/>10 /// </summary>11 public class ScreenshotOptions12 {13 private static readonly Dictionary<string, ScreenshotType?> _extensionScreenshotTypeMap = new Dictionary<string, ScreenshotType?>14 {15 ["jpe"] = ScreenshotType.Jpeg,16 ["jpeg"] = ScreenshotType.Jpeg,17 ["jpg"] = ScreenshotType.Jpeg,18 ["png"] = ScreenshotType.Png,19 };20 /// <summary>21 /// Specifies clipping region of the page.22 /// </summary>23 /// <value>The clip.</value>24 public Clip Clip { get; set; }25 /// <summary>26 /// When <c>true</c>, takes a screenshot of the full scrollable page. Defaults to <c>false</c>.27 /// </summary>28 /// <value><c>true</c> if full page; otherwise, <c>false</c>.</value>29 public bool FullPage { get; set; }30 /// <summary>31 /// Hides default white background and allows capturing screenshots with transparency. Defaults to <c>false</c>32 /// </summary>33 /// <value><c>true</c> if omit background; otherwise, <c>false</c>.</value>34 public bool OmitBackground { get; set; }35 /// <summary>36 /// Specify screenshot type, can be either jpeg or png. Defaults to 'png'.37 /// </summary>38 /// <value>The type.</value>39 public ScreenshotType? Type { get; set; }40 /// <summary>41 /// The quality of the image, between 0-100. Not applicable to png images.42 /// </summary>43 /// <value>The quality.</value>44 public int? Quality { get; set; }45 /// <summary>46 /// When BurstMode is <c>true</c> the screenshot process will only execute all the screenshot setup actions (background and metrics overrides)47 /// before the first screenshot call and it will ignore the reset actions after the screenshoot is taken.48 /// <see cref="Page.SetBurstModeOffAsync"/> needs to be called after the last screenshot is taken.49 /// </summary>50 /// <example><![CDATA[51 /// var screenShotOptions = new ScreenshotOptions52 /// {53 /// FullPage = true,54 /// BurstMode = true55 /// };56 /// await page.GoToAsync("https://www.google.com");57 /// for(var x = 0; x < 100; x++)58 /// {59 /// await page.ScreenshotBase64Async(screenShotOptions);60 /// }61 /// await page.SetBurstModeOffAsync();62 /// ]]></example>63 [JsonIgnore]64 public bool BurstMode { get; set; } = false;65 internal static ScreenshotType? GetScreenshotTypeFromFile(string file)...
ScreenshotLoader.cs
Source:ScreenshotLoader.cs
...40 Width = Options.Value.ScreenshotWidth, 41 Height = Options.Value.ScreenshotHeight 42 });43 await page.GoToAsync(source);44 byte[] buffer = await page.ScreenshotDataAsync(new ScreenshotOptions() { Type = ScreenshotType.Png });45 return new OriginalData(MimeTypes.Png, new MemoryStream(buffer), new CacheSettings().ApplyLoaderOptions(Options.Value));46 }47}...
Program.cs
Source:Program.cs
...21 //设置æµè§å¨ç页é¢å¤§å°ð22 // await page.SetViewportAsync(new ViewPortOptions { Width = 1024, Height = 768 });23 await page.GoToAsync("http://www.baidu.com");24 // var html = await page.GetContentAsync();25 var stream = await page.ScreenshotStreamAsync(new ScreenshotOptions { FullPage = true, Type = ScreenshotType.Png });26 //å°é¡µé¢ä¿å为jpgå¾ç27 await page.ScreenshotAsync($@"{System.IO.Directory.GetCurrentDirectory()}\page_{Guid.NewGuid().ToString()}.jpg", new ScreenshotOptions() { FullPage = true, Type = ScreenshotType.Jpeg });28 //å°é¡µé¢ä¿å为pngå¾ç29 await page.ScreenshotAsync($@"{System.IO.Directory.GetCurrentDirectory()}\page_{Guid.NewGuid().ToString()}.png", new ScreenshotOptions() { FullPage = true, Type = ScreenshotType.Png });30 //å°é¡µé¢ä¿å为pdfæ件31 await page.PdfAsync($@"{System.IO.Directory.GetCurrentDirectory()}\page_{Guid.NewGuid().ToString()}_page.pdf");32 Console.WriteLine("为ä»ä¹ä¸è¡å¢ /åå¼å¼");33 }34 }35 }36 }37}...
StrategyPNG.cs
Source:StrategyPNG.cs
...12 {13 try14 { 15 // Configures the screenshot16 ScreenshotOptions screenshotOptions = new ScreenshotOptions17 {18 Type = ScreenshotType.Png,19 OmitBackground = true20 };21 // The file path to be saved22 string filename = $"{Environment.CurrentDirectory}/wwwroot/{DateTime.Now.ToString("yyyyMMdd_hhmmss")}.png";23 await PuppeteerPage.ScreenshotAsync(filename, screenshotOptions);24 25 // returns only the last element (filename + extension).26 return filename.Split('/')[^1];27 }28 catch (AggregateException ex)29 {30 return $"Couldn't generate the content due to error {ex.Message}";...
IPdfRenderer.cs
Source:IPdfRenderer.cs
...5public interface IPdfRenderer6{7 Task<byte[]> GetPdfByUrlAsync(string url, PdfOptions? options = null, TimeSpan? delay = null);8 Task<byte[]> GetPdfByHtmlAsync(string html, PdfOptions? options = null, TimeSpan? delay = null);9 Task<byte[]> GetScreenshotByUrlAsync(string url, ScreenshotOptions? options = null, TimeSpan? delay = null);10 Task<byte[]> GetScreenshotByHtmlAsync(string html, ScreenshotOptions? options = null, TimeSpan? delay = null);11}...
ScreenshotOptions
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.IO;4using System.Threading.Tasks;5{6 {7 static async Task Main(string[] args)8 {9 await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);10 var options = new LaunchOptions { Headless = true };11 using (var browser = await Puppeteer.LaunchAsync(options))12 {13 using (var page = await browser.NewPageAsync())14 {15 await page.ScreenshotAsync("google.png", new ScreenshotOptions { FullPage = true });16 }17 }18 }19 }20}
ScreenshotOptions
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using PuppeteerSharp;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 TakeScreenshot().Wait();10 }11 static async Task TakeScreenshot()12 {13 var options = new LaunchOptions { Headless = true };14 using (var browser = await Puppeteer.LaunchAsync(options))15 using (var page = await browser.NewPageAsync())16 {17 await page.ScreenshotAsync("google.png");18 }19 }20 }21}22using System;23using System.Threading.Tasks;24using PuppeteerSharp;25{26 {27 static void Main(string[] args)28 {29 Console.WriteLine("Hello World!");30 TakeScreenshot().Wait();31 }
ScreenshotOptions
Using AI Code Generation
1ScreenshotOptions options = new ScreenshotOptions()2{3};4await page.ScreenshotAsync("screenshot.jpg", options);5ScreenshotOptions options = new ScreenshotOptions()6{7};8await page.ScreenshotAsync("screenshot.png", options);9ScreenshotOptions options = new ScreenshotOptions()10{11};12await page.ScreenshotAsync("screenshot.webp", options);13ScreenshotOptions options = new ScreenshotOptions()14{15};16await page.ScreenshotAsync("screenshot.jpg", options);17ScreenshotOptions options = new ScreenshotOptions()18{19};20await page.ScreenshotAsync("screenshot.png", options);21ScreenshotOptions options = new ScreenshotOptions()22{23};24await page.ScreenshotAsync("screenshot.webp", options);25ScreenshotOptions options = new ScreenshotOptions()26{27};28await page.ScreenshotAsync("screenshot.jpg", options);29ScreenshotOptions options = new ScreenshotOptions()30{31};32await page.ScreenshotAsync("screenshot.png
ScreenshotOptions
Using AI Code Generation
1using PuppeteerSharp;2using System;3using System.Threading.Tasks;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 TakeScreenshotAsync().Wait();10 }11 public static async Task TakeScreenshotAsync()12 {13 {14 };15 using (var browser = await Puppeteer.LaunchAsync(options))16 using (var page = await browser.NewPageAsync())17 {18 await page.ScreenshotAsync("google.png");19 }20 }21 }22}
ScreenshotOptions
Using AI Code Generation
1using System;2using System.IO;3using System.Threading.Tasks;4using PuppeteerSharp;5using PuppeteerSharp.Extensions;6using PuppeteerSharp.Contrib.Extensions;7using PuppeteerSharp.Contrib.Extensions.Pdf;8using PuppeteerSharp.Contrib.Extensions.Pdf.Options;9{10 {11 public static ScreenshotOptions WithFullPage(this ScreenshotOptions options)12 {13 options.FullPage = true;14 return options;15 }16 }17}18using System;19using System.IO;20using System.Threading.Tasks;21using PuppeteerSharp;22using PuppeteerSharp.Extensions;23using PuppeteerSharp.Contrib.Extensions;24using PuppeteerSharp.Contrib.Extensions.Pdf;25using PuppeteerSharp.Contrib.Extensions.Pdf.Options;26{27 {28 public static ScreenshotOptions WithFullPage(this ScreenshotOptions options)29 {30 options.FullPage = true;31 return options;32 }33 }34}35using System;36using System.IO;37using System.Threading.Tasks;38using PuppeteerSharp;
ScreenshotOptions
Using AI Code Generation
1{2};3var screenshot = await page.ScreenshotDataAsync(options);4var base64 = Convert.ToBase64String(screenshot);5File.WriteAllText("Screenshot.txt", base64);6{7};8var screenshot = await page.ScreenshotDataAsync(options);9var base64 = Convert.ToBase64String(screenshot);10File.WriteAllText("Screenshot.txt", base64);11{12};13var screenshot = await page.ScreenshotDataAsync(options);14var base64 = Convert.ToBase64String(screenshot);15File.WriteAllText("Screenshot.txt", base64);16{17};18var screenshot = await page.ScreenshotDataAsync(options);
ScreenshotOptions
Using AI Code Generation
1var options = new ScreenshotOptions { FullPage = true };2await page.ScreenshotAsync("screenshot.png", options);3var options = new ScreenshotOptions { FullPage = true };4await page.ScreenshotAsync("screenshot.png", options);5var options = new ScreenshotOptions { FullPage = true };6await page.ScreenshotAsync("screenshot.png", options);7var options = new ScreenshotOptions { FullPage = true };8await page.ScreenshotAsync("screenshot.png", options);9var options = new ScreenshotOptions { FullPage = true };10await page.ScreenshotAsync("screenshot.png", options);11var options = new ScreenshotOptions { FullPage = true };12await page.ScreenshotAsync("screenshot.png", options);13var options = new ScreenshotOptions { FullPage = true };14await page.ScreenshotAsync("screenshot.png", options);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!