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": ""

Image generated with C#. Convert HTML to an image using 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 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(
                    new System.Collections.Specialized.NameValueCollection()
                        { "html", html }, { "css", css }
            string resultString = System.Text.Encoding.UTF8.GetString(result);
            // {"url":""}

Need help?

We’re always looking to improve this documentation. Please send us an email: We respond fast.

Back to top

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

Page last modified: Apr 9 2024 at 11:04 PM.

Edit this page on GitHub.