Code Examples

Take Screenshots with C#

Ready-to-copy C# code for capturing website screenshots, generating PDFs, and processing pages at scale. Uses HttpClient (.NET 6+).

Quick Start

Screenshot in C# — minimal example

The simplest way to capture a screenshot. Just a URL and your API key.

C#quickstart
using var client = new HttpClient();
var response = await client.GetAsync(
    "https://api.screenshotdev.com/v1/screenshot?url=https://example.com&access_key=YOUR_ACCESS_KEY"
);
var bytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("screenshot.png", bytes);
Full Example

Production-ready C# script

Complete working script with options for viewport, format, full page capture, and error handling.

C#full example
using System.Net.Http;
using System.Web;

const string ApiKey = "YOUR_ACCESS_KEY";
const string BaseUrl = "https://api.screenshotdev.com/v1/screenshot";

static async Task<byte[]> TakeScreenshot(
    string url,
    string format = "webp",
    int width = 1280,
    bool fullPage = false,
    bool darkMode = false)
{
    using var client = new HttpClient();
    var query = HttpUtility.ParseQueryString(string.Empty);
    query["url"] = url;
    query["access_key"] = ApiKey;
    query["format"] = format;
    query["width"] = width.ToString();
    query["full_page"] = fullPage.ToString().ToLower();
    query["dark_mode"] = darkMode.ToString().ToLower();

    var response = await client.GetAsync($"{BaseUrl}?{query}");
    if (!response.IsSuccessStatusCode)
    {
        var body = await response.Content.ReadAsStringAsync();
        throw new HttpRequestException($"Screenshot failed ({response.StatusCode}): {body}");
    }
    return await response.Content.ReadAsByteArrayAsync();
}

var img = await TakeScreenshot("https://example.com", format: "webp", width: 1440, fullPage: true);
await File.WriteAllBytesAsync("screenshot.webp", img);
Console.WriteLine($"Saved screenshot.webp ({img.Length} bytes)");
Advanced

More C# examples

PDF generation, batch processing, webhooks, CSS/JS injection, and S3 upload.

PDF Generation

C#pdf
using var client = new HttpClient();
var query = HttpUtility.ParseQueryString(string.Empty);
query["url"] = "https://example.com";
query["access_key"] = "YOUR_ACCESS_KEY";
query["format"] = "a4";
query["landscape"] = "false";
query["margin"] = "20px";

var response = await client.GetAsync($"https://api.screenshotdev.com/v1/pdf?{query}");
var bytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("page.pdf", bytes);

Batch Screenshots

C#batch
var urls = new[] {
    "https://example.com",
    "https://github.com",
    "https://news.ycombinator.com",
};

using var client = new HttpClient();
var tasks = urls.Select(async url => {
    var query = HttpUtility.ParseQueryString(string.Empty);
    query["url"] = url;
    query["access_key"] = ApiKey;
    query["format"] = "webp";

    var response = await client.GetAsync($"{BaseUrl}?{query}");
    var bytes = await response.Content.ReadAsByteArrayAsync();
    var host = new Uri(url).Host;
    await File.WriteAllBytesAsync($"{host}.webp", bytes);
    Console.WriteLine($"  {host}.webp ({bytes.Length:N0} bytes)");
});
await Task.WhenAll(tasks);

Async with Webhooks

C#webhooks
using var client = new HttpClient();
var json = JsonSerializer.Serialize(new {
    url = "https://example.com",
    access_key = "YOUR_ACCESS_KEY",
    webhook_url = "https://your-app.com/api/webhooks/screenshot",
    format = "webp",
    full_page = true,
});
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PostAsync("https://api.screenshotdev.com/v1/screenshot", content);
var result = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Job queued: {result}");

Custom CSS/JS Injection

C#inject
var query = HttpUtility.ParseQueryString(string.Empty);
query["url"] = "https://example.com";
query["access_key"] = "YOUR_ACCESS_KEY";
query["inject_css"] = "body { font-family: 'Inter', sans-serif; } .ad { display: none; }";
query["inject_js"] = "document.querySelectorAll('[data-cookie-banner]').forEach(el => el.remove())";
query["block_ads"] = "true";

using var client = new HttpClient();
var response = await client.GetAsync($"{BaseUrl}?{query}");
var bytes = await response.Content.ReadAsByteArrayAsync();
await File.WriteAllBytesAsync("clean.webp", bytes);

S3 Upload

C#s3
using Amazon.S3;
using Amazon.S3.Model;

// 1. Capture
var img = await TakeScreenshot("https://example.com", format: "webp");

// 2. Upload
var s3 = new AmazonS3Client();
await s3.PutObjectAsync(new PutObjectRequest {
    BucketName = "my-screenshots",
    Key = "captures/example-com.webp",
    InputStream = new MemoryStream(img),
    ContentType = "image/webp",
});

Try it live

Test the API in the interactive playground — pre-filled with C# snippets.

Setup

Installation

Install the HTTP library used in the examples above.

Terminalinstall
# No dependencies — uses System.Net.Http from .NET 6+

Uses HttpClient (.NET 6+). Any HTTP client that supports GET requests will work.

More Examples

Code examples in other languages

Start capturing screenshots
with C# today.

100 free API calls per month. No credit card. First screenshot in under 30 seconds.

Take Screenshots with C# — ScreenshotDEV