How to take website screenshots with C# (.NET)

Published on Dmytro Krasun 3 min read
Today, there are many options to make screenshots of any URL with C# (.NET). Let's examine them all and choose which suits you best.

Let’s check out:

  1. Puppeteer Sharp client for C# (.NET).
  2. Or Screenshot API as a service.

They might overlap, but there is no best solution. Each depends on your use case and requirements.

Puppeteer Sharp client for C# (.NET)

To take screenshots of any URL in the modern version of C#, you can use a headless browser library like PuppeteerSharp. Puppeteer Sharp is a .NET port of the popular JavaScript library Puppeteer, which controls headless Chromium or Chrome browsers.

In your .NET project, add the Puppeteer Sharp package using the NuGet Package Manager, or run the following command in the Package Manager Console:

Terminal window
Install-Package PuppeteerSharp

Create a new C# file and add the following code, replacing https://example.com with the URL you want to take a screenshot of, and “screenshot.png” with the desired output file name.

using System;
using System.Threading.Tasks;
using PuppeteerSharp;
namespace ScreenshotExample
{
class Program
{
static async Task Main(string[] args)
{
// Download the Chromium browser if needed
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
// Launch the browser
using var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = true
});
// Create a new page
using var page = await browser.NewPageAsync();
// Navigate to the URL
await page.GoToAsync("https://example.com");
// Set the viewport size if needed (optional)
await page.SetViewportAsync(new ViewPortOptions
{
Width = 1920,
Height = 1080
});
// Take the screenshot
await page.ScreenshotAsync("screenshot.png");
// Close the browser
await browser.CloseAsync();
Console.WriteLine("Screenshot saved!");
}
}
}

Execute the program, and it will take a screenshot of the specified URL and save it as an image file in the project’s output directory. Remember to replace the URL and output file name with the desired values.

If you just want to take one or two screenshots locally, the PuppeteerSharp client is not the best fit. But if you plan to take millions of screenshots and manage browser instances, you can do it yourself, but it is better to outsource to well-established services.

Screenshot API as a service

ScreenshotOne specializes in taking screenshots and managing browser instances at scale. The service provides a high-quality C# (.NET) client to take screenshots and cover a variety of use cases.

Massive thanks and rays of goodness to Andy Robinson (Indie Hackers, GitHub) for providing the fully-featured high-quality C# (.NET) SDK.

It is easy to install. You can add the library via nuget using the package manager console:

Terminal window
PM> Install-Package ScreenshotOne.dotnetsdk

Or from the .NET CLI as:

Terminal window
dotnet add package ScreenshotOne.dotnetsdk

Don’t forget to sign up to get access and secret keys.

Generate a screenshot URL without executing request:

var client = new Client("<access key>', '<secret key>");
var options = TakeOptions.Url("https://www.amazon.com")
.FullPage(true)
.Format(./Format.PNG)
.BlockCookieBanners(true);
var url = client.GenerateTakeUrl(options);
// url = https://api.screenshotone.com/take?url=https%3A%2F%2Fwww.amazon.com&full_page=true&format=png&block_cookie_banners=true&access_key=_OzqMIjpCw-ARQ&signature=8a08e62d13a5c3490fda0734b6707791d3decc9ab9ba41e8cc045288a39db502

Take a screenshot and save the image in the file:

var client = new Client("<access key>', '<secret key>");
var options = TakeOptions.Url("https://www.google.com")
.FullPage(true)
.Format(./Format.PNG)
.BlockCookieBanners(true);
var bytes = await client.Take(options);
File.WriteAllBytes(@"c:\temp\example.png", bytes);

If you feel that it is the best fit for you, feel free to sign up for our screenshot API and get the access key.

Summary

Pick the solution which suits your needs best. If you decide to go with our API, please ask any questions and mail us at support@screenshotone.com. And have a nice day 👋

By the way, you might also find interesting how to: