Skip to content
shotsmith
Features Docs Guides Pricing Changelog Sign inStart free

How to take website screenshots in C# and .NET

Capture website screenshots and PDFs from C# with HttpClient, plus an ASP.NET Core minimal API endpoint that returns the image.

3 min read

Taking a screenshot of a web page in .NET usually means PuppeteerSharp or Playwright, and a Chromium download on every server and container you deploy. With shotsmith, the browser runs on our side and you use the HttpClient you already have.

1. Get an API key

Create a free account (100 renders a month, no card). Store the key with user secrets in development, and in your host’s configuration in production, for example as Shotsmith:AccessKey.

2. Save a screenshot

This uses top-level statements (.NET 6 and later). GetByteArrayAsync throws on error responses, so a failed render never gets written to disk:

using System;
using System.IO;
using System.Net.Http;

using var http = new HttpClient { Timeout = TimeSpan.FromSeconds(120) };

// Throws HttpRequestException on a 4xx/5xx response
var bytes = await http.GetByteArrayAsync(
    "https://shotsmith.dev/v1/take?access_key=YOUR_ACCESS_KEY&url=https%3A%2F%2Fshotsmith.dev%2Fsample&full_page=true&block_cookie_banners=true&format=png"
);

await File.WriteAllBytesAsync("capture.png", bytes);

3. An ASP.NET Core minimal API endpoint

Register a named HttpClient through IHttpClientFactory, so connections are pooled properly, and return the image from an endpoint. Errors pass through with their original status and JSON body.

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHttpClient("shotsmith", client =>
{
    client.BaseAddress = new Uri("https://shotsmith.dev/");
    client.Timeout = TimeSpan.FromSeconds(30);
});
var app = builder.Build();

// GET /thumbnail?url=https://shotsmith.dev/sample
app.MapGet("/thumbnail", async (string url, IHttpClientFactory factory) =>
{
    var query = $"v1/take?access_key={Uri.EscapeDataString(builder.Configuration["Shotsmith:AccessKey"]!)}"
        + $"&url={Uri.EscapeDataString(url)}"
        + "&format=webp&image_width=640&block_cookie_banners=true&cache=true";

    using var res = await factory.CreateClient("shotsmith").GetAsync(query);
    var body = await res.Content.ReadAsByteArrayAsync();
    var contentType = res.Content.Headers.ContentType?.ToString() ?? "application/octet-stream";

    // On errors the body is JSON with error_code and error_message: pass it on with the same status.
    return Results.Bytes(body, contentType, statusCode: (int)res.StatusCode);
});

app.Run();

Every call to this endpoint spends your renders. Limit url to the hosts you expect, or require authentication with .RequireAuthorization().

4. PDFs from your own HTML

Send your Razor-rendered HTML as JSON and save the PDF that comes back:

using System;
using System.IO;
using System.Net.Http;
using System.Text;

using var http = new HttpClient();
http.DefaultRequestHeaders.Add("X-Access-Key", "YOUR_ACCESS_KEY");

var body = """
    {
      "html": "<h1>Invoice #1042</h1>",
      "format": "pdf",
      "pdf_paper_format": "a4",
      "pdf_print_background": true
    }
    """;

var res = await http.PostAsync(
    "https://shotsmith.dev/v1/take",
    new StringContent(body, Encoding.UTF8, "application/json")
);
res.EnsureSuccessStatusCode();

await File.WriteAllBytesAsync("invoice.pdf", await res.Content.ReadAsByteArrayAsync());

For real templates, serialise the request with System.Text.Json (JsonContent.Create(new { html, format = "pdf" })) instead of a raw string, so quotes and newlines are escaped for you.

5. Errors and long renders

Errors are JSON with a stable error_code, listed in the error reference, and failed renders aren’t billed. Synchronous requests must finish within 18 seconds, so set your HttpClient timeout to around 30. For slower pages and videos, use async rendering with webhooks.