Link Search Menu Expand Document

C#: HTML/CSS to Image

Generate a png, jpg or webp images with C#. Renders exactly like Google Chrome.

Live demo Get an API Key


Table of contents

Generating images with C#

  1. The API takes your HTML/CSS and runs it inside a real instance of Google Chrome to convert your html into an image.
  2. Use C# to send the API your HTML/CSS.
  3. 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"
}

Image generated with C#.

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.

Free API Key for C#


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.


Back to top

Built with extensive integration tests and serious care for developer happiness.
© 2018-2020 Code Happy, LLC.

Page last modified: Nov 23 2020 at 05:16 PM.

Edit this page on GitHub.