Skip to content

Simple example how to call screenshot machine's API using C# programming language.

License

Notifications You must be signed in to change notification settings

screenshotmachine/screenshotmachine-csharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

screenshotmachine-csharp

This demo shows how to call online screenshot machine API using C# programming language.

Installation

First, you need to create a free/premium account at www.screenshotmachine.com website. After registration, you will see your customer key in your user profile. Also secret phrase is maintained here. Please, use secret phrase always, when your API calls are called from publicly available websites.

Set-up your customer key and secret phrase (if needed) in the script:

    string customerKey = "PUT_YOUR_CUSTOMER_KEY_HERE";
    string secretPhrase = ""; //leave secret phrase empty, if not needed

Website screenshot API

Set additional options to fulfill your needs:

    var options = new Dictionary<string, string>();
    // mandatory parameter
    options.Add("url", "https://www.google.com");
    // all next parameters are optional, see our API guide for more details
    options.Add("dimension", "1366x768"); // or "1366xfull" for full length screenshot
    options.Add("device", "desktop");
    options.Add("format", "png");
    options.Add("cacheLimit", "0");
    options.Add("delay", "200");
    options.Add("zoom", "100");

More info about options can be found in our Website screenshot API.

Sample code

using System;
using System.Net;
using System.Collections.Generic;

class Client
{
    static void Main(string[] args)
    {
        string customerKey = "PUT_YOUR_CUSTOMER_KEY_HERE";
        string secretPhrase = ""; //leave secret phrase empty, if not needed

        var options = new Dictionary<string, string>();
        // mandatory parameter
        options.Add("url", "https://www.google.com");
        // all next parameters are optional, see our webtite screenshot API guide for more details
        options.Add("dimension", "1366x768"); // or "1366xfull" for full length screenshot
        options.Add("device", "desktop");
        options.Add("format", "png");
        options.Add("cacheLimit", "0");
        options.Add("delay", "200");
        options.Add("zoom", "100");

        ScreenshotMachine sm = new ScreenshotMachine(customerKey, secretPhrase);

        string apiUrl = sm.GenerateScreenshotApiUrl(options);
        //use final apiUrl where needed
        Console.WriteLine(apiUrl);
    }
} 

Generated apiUrl link can be placed in <img> tag or used in your business logic later.

If you need to store captured screenshot as an image, just call:

    string apiUrl = sm.GenerateScreenshotApiUrl(options);

    //or save screenshot directly
    string output = "output.png";
    new WebClient().DownloadFile(apiUrl, output);
    Console.WriteLine("Screenshot saved as " + output);

Captured screenshot will be saved as output.png file in the current directory.

Website to PDF API

Set the PDF options:

    var options = new Dictionary<string, string>();
    // mandatory parameter
    options.Add("url", "https://www.google.com");
    // all next parameters are optional, see our website to PDF API guide for more details
    options.Add("paper", "letter");
    options.Add("orientation", "portrait");
    options.Add("media", "print");
    options.Add("bg", "nobg");
    options.Add("delay", "2000");
    options.Add("scale", "50");

More info about options can be found in our Website to PDF API.

Sample code

using System;
using System.Net;
using System.Collections.Generic;

class ClientPdf
{
    static void Main(string[] args)
    {
        string customerKey = "PUT_YOUR_CUSTOMER_KEY_HERE";
        string secretPhrase = ""; //leave secret phrase empty, if not needed

        var options = new Dictionary<string, string>();
        // mandatory parameter
        options.Add("url", "https://www.google.com");
        // all next parameters are optional, see our website to PDF API guide for more details
        options.Add("paper", "letter");
        options.Add("orientation", "portrait");
        options.Add("media", "print");
        options.Add("bg", "nobg");
        options.Add("delay", "2000");
        options.Add("scale", "50");

        ScreenshotMachine sm = new ScreenshotMachine(customerKey, secretPhrase);

        string pdfApiUrl = sm.GeneratePdfApiUrl(options);

        //save PDF file
        string output = "output.pdf";
        new WebClient().DownloadFile(pdfApiUrl, output);
        Console.Write("Pdf saved as " + output);
    }
}

Captured PDF will be saved as output.pdf file in the current directory.

License

The MIT License (MIT)