C#: HTML/CSS to Image
Generate a png, jpg or webp images with C#. Renders exactly like Google Chrome.
Table of contents
Generating images with C#
- The API takes your HTML/CSS and runs it inside a real instance of Google Chrome to convert your html into an image.
- Use C# to send the API your HTML/CSS.
- You’ll get back json with the URL to your generated image.
For more details on how this works, see Creating an image.
Example API response:
{
"url": "https://hcti.io/v1/image/be4c5118-fe19-462b-a49e-48cf72697a9d"
}
Authentication with C#
The API uses HTTP Basic authentication.
Your username is your User ID and your password is your API Key. Both of these are available from the dashboard. The C# code sample demonstrates how to authenticate your request.
You can signup for a free API key to get started.
C# example code
This C# code example sends an HTTP POST to the https://hcti.io/v1/image
API. Converting your HTML/CSS to an image with C#.
This code creates a WebClient, sets credentials and POSTS the HTML & CSS as values to the API. The response will be json
with the URL to the generated image.
using System.Net;
namespace htciExample
{
class Program
{
static void Main()
{
byte[] result;
string html = "<div class='ping'>Pong ✅</div>";
string css = ".ping {padding: 20px; font-family:'sans-serif'; }";
using (var client = new WebClient())
{
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("user_id:api_key"));
client.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
result = client.UploadValues(
"https://hcti.io/v1/image",
"POST",
new System.Collections.Specialized.NameValueCollection()
{
{ "html", html }, { "css", css }
}
);
}
string resultString = System.Text.Encoding.UTF8.GetString(result);
Console.WriteLine(resultString);
// {"url":"https://hcti.io/v1/image/404ed70d-dcb9-4778-9be4-fad912321d5b"}
}
}
}
Need help?
We’re always looking to improve this documentation. Please send us an email: support@htmlcsstoimage.com. We respond fast.