How to use ScreenshotOptions class of PuppeteerSharp package

Best Puppeteer-sharp code snippet using PuppeteerSharp.ScreenshotOptions

LottoXClient.cs

Source:LottoXClient.cs Github

copy

Full Screen

...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}...

Full Screen

Full Screen

SaveWebPage.xaml.cs

Source:SaveWebPage.xaml.cs Github

copy

Full Screen

...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); ...

Full Screen

Full Screen

Form1.cs

Source:Form1.cs Github

copy

Full Screen

...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选项...

Full Screen

Full Screen

ScreenshotOptions.cs

Source:ScreenshotOptions.cs Github

copy

Full Screen

...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)...

Full Screen

Full Screen

ScreenshotLoader.cs

Source:ScreenshotLoader.cs Github

copy

Full Screen

...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}...

Full Screen

Full Screen

Program.cs

Source:Program.cs Github

copy

Full Screen

...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}...

Full Screen

Full Screen

StrategyPNG.cs

Source:StrategyPNG.cs Github

copy

Full Screen

...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}";...

Full Screen

Full Screen

IPdfRenderer.cs

Source:IPdfRenderer.cs Github

copy

Full Screen

...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}...

Full Screen

Full Screen

ScreenshotOptions

Using AI Code Generation

copy

Full Screen

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}

Full Screen

Full Screen

ScreenshotOptions

Using AI Code Generation

copy

Full Screen

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 }

Full Screen

Full Screen

ScreenshotOptions

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

ScreenshotOptions

Using AI Code Generation

copy

Full Screen

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}

Full Screen

Full Screen

ScreenshotOptions

Using AI Code Generation

copy

Full Screen

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;

Full Screen

Full Screen

ScreenshotOptions

Using AI Code Generation

copy

Full Screen

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);

Full Screen

Full Screen

ScreenshotOptions

Using AI Code Generation

copy

Full Screen

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);

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer-sharp automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful