There are several ways that the GrabzIt API can be used to take screenshots and more in ASP.NET. To get started with the API follow these steps:
This .NET Standard library can be used in any .NET compliant setting, so Windows Apps, Linux Apps, .NET, ASP.NET. As well as other .NET languages like Visual Basic .NET, A# or J#. Simply reference the GrabzIt package in your project to get full access to all the features of the GrabzIt library.
To use the API you will first need to create an instance of the GrabzItClient class, passing your application key and application secret from your GrabzIt account to the constructor. As shown in the below example, note that while all of our ASP.NET examples are written in C# any ASP.NET language will work with this library.
For thread saftey it is best to create a new instance of this GrabzItClient
class for every new capture. While it is not required to pass a HttpClient
to the GrabzItClient
class, it is highly recommended as it will make your app use less system resources.
//Only keep one instance of the http client HttpClient client = new HttpClient(); //Create the GrabzItClient class //Replace "APPLICATION KEY", "APPLICATION SECRET" with the values from your account! GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret", client);
Then use one of the image, PDF, DOCX, animation or table options to capture a URL or convert HTML.
// To take a image screenshot grabzIt.URLToImage("https://www.tesla.com"); // Or to take a PDF screenshot grabzIt.URLToPDF("https://www.tesla.com"); // Or to convert online videos into animated GIF's grabzIt.URLToAnimation("http://www.example.com/video.avi"); // Or to capture table(s) grabzIt.URLToTable("https://www.tesla.com");
// To convert HTML into a image grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>"); // Or convert HTML into a PDF document grabzIt.HTMLToPDF("<html><body><h1>Hello World!</h1></body></html>"); // Or convert HTML into a CSV, XLSX or JSON document grabzIt.HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr> <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr> </table></body></html>");
// To convert HTML file into a image grabzIt.FileToImage("example.html"); // Or convert HTML file into a PDF document grabzIt.FileToPDF("example.html"); // Or convert HTML file into a CSV, XLSX or JSON document grabzIt.FileToTable("tables.html");
Next call either the SaveAsync
or SaveToAsync
method. Below is an example of the calling the SaveAsync
method, this example include the URL to the handler, which will process the callback from the service and save the resulting capture. As this method requires a callback handler it is usually only possible to use this approach in a web application.
await grabzIt.SaveAsync("http://www.example.com/Home/Handler");
Alternatively the synchonous SaveToAsync method, this will force your application to wait while the screenshot is created so it should only be used were the SaveAsync method cannot be used, such as in a desktop application.
Once the screenshot is created it is saved to the file path provided.
string filepath = "images/result.jpg"; await grabzIt.SaveToAsync(filepath);
All of our services such as creating web captures, animated GIF's or converting HTML have many customizable options including; configuring the browser height, browser width, screenshot height, screenshot width, the delay before a screenshot is taken and the image format of the screenshot. Refer to the Client documentation for details on all these options as well as more advanced examples.