How to make screenshots in C#
You can capture a web page from C# by sending a request with one of three screenshot options.
Choose the option that matches the area you need: only what is on screen, the entire page from top to bottom, or a single HTML element identified by its CSS selector. The examples below use a blog home page as the target and save the returned image to a local PNG file.
Capture only the visible viewport
Start with the screenshot parameter set to true. This returns an image of the portion of the page that fits inside the current viewport, not everything below the fold. In the worked example, the default viewport produced a 1920 by 1080 image that showed the top of the blog home page but excluded later blog posts and the footer. The complete C# request and file-writing flow for this mode is provided in protected module code0001.
In a modern .NET console program, set API_BASE_URL to the API origin, API_KEY to your key and SITE_BASE_URL to the website origin. The request captures that website’s /blog/ page. This helper reads the complete image response before saving it.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
string apiOrigin = Environment.GetEnvironmentVariable("API_BASE_URL")
?? throw new InvalidOperationException("Set API_BASE_URL");
string apiKey = Environment.GetEnvironmentVariable("API_KEY")
?? throw new InvalidOperationException("Set API_KEY");
string siteOrigin = Environment.GetEnvironmentVariable("SITE_BASE_URL")
?? throw new InvalidOperationException("Set SITE_BASE_URL");
using var transport = new HttpClient();
async Task SaveCaptureAsync(Dictionary<string, string> options, string outputFile)
{
options["api_key"] = apiKey;
options["url"] = siteOrigin.TrimEnd('/') + "/blog/";
string query = string.Join("&", options.Select(item =>
Uri.EscapeDataString(item.Key) + "=" + Uri.EscapeDataString(item.Value)));
byte[] image = await transport.GetByteArrayAsync(
apiOrigin.TrimEnd('/') + "/api/v1/?" + query);
await File.WriteAllBytesAsync(outputFile, image);
}
await SaveCaptureAsync(new Dictionary<string, string>
{
["screenshot"] = "true"
}, "viewport.png");Adjust the viewport size
Viewport size controls how much of the page is visible in a standard screenshot. The default is 1920 pixels wide by 1080 pixels high. Add window_width and window_height to the same request to change that framing. For example, setting width to 1280 and height to 720 produces a 1280 by 720 image. Use this adjustment when you need a specific screen size for comparison, layout checks, or downstream image processing. Protected module code0002 shows the same request with those two values added.
Use the SaveCaptureAsync helper defined in the visible-viewport example. These documented dimensions set the browser viewport rather than the full-page screenshot extent.
await SaveCaptureAsync(new Dictionary<string, string>
{
["screenshot"] = "true",
["window_width"] = "1280",
["window_height"] = "720"
}, "viewport-1280x720.png");Capture the entire page
Set screenshot_full_page to true when the visible area is not enough. This mode expands the capture beyond the initial viewport so the image covers the whole page, including content that would normally require scrolling. It is the right choice for archiving a complete page or reviewing everything on it in one file, while the plain screenshot option remains better when you only need the first screen. Protected module code0003 contains the full-page version of the request.
Use the same helper. The full-page variant sends screenshot_full_page rather than the screenshot option used for a viewport capture.
await SaveCaptureAsync(new Dictionary<string, string>
{
["screenshot_full_page"] = "true"
}, "full-page.png");Capture a single element
Use screenshot_selector when only one part of the page matters. Pass the CSS selector for the target element instead of a boolean. In the example, the selector footer captures just the footer section, which is useful for focused visual checks without downloading or cropping a larger image. Protected module code0004 shows the request with that selector applied.
Use the same helper and a page containing a footer element. screenshot_selector narrows the capture to the documented selector.
await SaveCaptureAsync(new Dictionary<string, string>
{
["screenshot_selector"] = "footer"
}, "footer.png");Quick decision checklist
Decide by output scope first: screenshot for the first screen, screenshot_full_page for everything, and screenshot_selector for one element. Then, for the first option only, check whether the default 1920 by 1080 viewport suits your purpose or whether window_width and window_height should be set. Finally, confirm the saved file matches the intended scope — a cropped top section, a long full-page image, or a small element image — before using it.