How to Convert HTML to PNG in C# .NET Core Framework

Cloudmersive
2 min readApr 24, 2024

We can easily convert HTML strings to PNG screenshots using a low-code API.

To structure our API call, we can copy from ready-to-run C# .NET Core code examples provided below. We’ll just need to authorize our request with a free API key, and we’ll be able to make up to 800 HTML to PNG conversions per month with zero commitments.

We can install the client SDK via NuGet by running the below command in our Package Manager console:

Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1

After that, we can copy the below code to call the function (our API key goes in the Configuration.Default.AddApiKey snippet):

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;

namespace Example
{
public class ConvertWebHtmlToPngExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ConvertWebApi();
var input = new HtmlToPngRequest(); // HtmlToPngRequest | HTML to PNG request parameters

try
{
// Convert HTML string to PNG screenshot
byte[] result = apiInstance.ConvertWebHtmlToPng(input);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertWebApi.ConvertWebHtmlToPng: " + e.Message );
}
}
}
}

When we structure an input request containing our HTML string, we can also specify the height and width of our screenshot. We can additionally determine a specific amount of extra loading time for our HTML content. This is reflected in the example JSON format request below:

{
"Html": "string",
"ExtraLoadingWait": 0,
"ScreenshotWidth": 0,
"ScreenshotHeight": 0
}

That’s all the code we’ll need! Now we can easily create PNG screenshots from HTML strings with just a few lines of code.

--

--

Cloudmersive

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