How to screenshot a website/URL using C# in .NET Framework

Screenshotting websites, while incredibly useful, can be a proper pain. Let’s look today at how to avoid the hassle of setting this up from scratch without using potentially expensive URL screenshotting services. Skipping further ado, let’s jump in.

First step, install our API client using NuGet via the Package Manager console.

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 1.3.4

Then, simply call ConvertWebUrlToScreenshot and provide it with a desired URL, desired dimensions, and additional load time if necessary.

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class ConvertWebUrlToScreenshotExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");
var apiInstance = new ConvertWebApi();
var input = new ScreenshotRequest(); // ScreenshotRequest | Screenshot request parameters
try
{
// Take screenshot of URL
byte[] result = apiInstance.ConvertWebUrlToScreenshot(input);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertWebApi.ConvertWebUrlToScreenshot: " + e.Message );
}
}
}
}

And that’s all there is to it! Here is an example input:

{
"Url": "google.com",
"ExtraLoadingWait": 0,
"ScreenshotWidth": 500,
"ScreenshotHeight": 500
}

And the corresponding output:

Image for post
Image for post

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store