- Docs
- Example code
- C# / .NET
C# / .NET
Generate PNG, JPG, or WebP images with C# and .NET, rendered in Google Chrome.
Live demo Get an API keyOfficial .NET Package
Section titled “Official .NET Package”We provide an official NuGet package for .NET that makes it easy to generate images from HTML/CSS in your .NET applications.
| Package | NuGet | Description |
|---|---|---|
| HtmlCssToImage | Core client library for the HTML/CSS to Image API | |
| HtmlCssToImage.DependencyInjection | ASP.NET Core dependency injection | |
| HtmlCssToImage.Blazor | Open Graph image tags for Blazor | |
| HtmlCssToImage.TagHelpers | Tag helpers for Razor Pages and MVC |
For full documentation, see the GitHub repository.
Quick Start
Section titled “Quick Start”1. Install the NuGet package
Section titled “1. Install the NuGet package”dotnet add package HtmlCssToImageOr using the Package Manager Console:
Install-Package HtmlCssToImage2. Create a client
Section titled “2. Create a client”using HtmlCssToImage;
// Retrieve your User ID and API Key from https://htmlcsstoimage.com/dashboardvar client = new HtmlCssToImageClient("your-user-id", "your-api-key");3. Generate an image
Section titled “3. Generate an image”var request = new CreateHtmlCssImageRequest{ Html = "<div class='box'>Hello, World!</div>", Css = ".box { padding: 20px; background: #03B875; color: white; font-family: 'Roboto'; }", GoogleFonts = "Roboto"};
using var result = await client.CreateImageAsync(request);
if (result.Success){ Console.WriteLine($"Image URL: {result.Response.Url}"); // https://hcti.io/v1/image/be4c5118-fe19-462b-a49e-48cf72697a9d}API calls return an ApiResult<T> that owns the raw HttpResponseMessage. The raw response lets you inspect headers, the originating request, and other HTTP details when needed. Use using or using var so it is disposed after you finish reading the result.
Creating Images from a URL
Section titled “Creating Images from a URL”Capture a screenshot of any webpage:
var request = new CreateUrlImageRequest{ Url = "https://htmlcsstoimage.com", FullScreen = true // Capture the full scrollable page};
using var result = await client.CreateImageAsync(request);
if (result.Success){ Console.WriteLine($"Screenshot URL: {result.Response.Url}");}Using Templates
Section titled “Using Templates”Generate images from pre-defined templates with dynamic values:
var request = new CreateTemplatedImageRequest{ TemplateId = "your-template-id", TemplateValues = new Dictionary<string, string> { { "title", "Welcome!" }, { "subtitle", "This is a templated image" } }};
using var result = await client.CreateImageAsync(request);ASP.NET Core Integration
Section titled “ASP.NET Core Integration”For ASP.NET Core applications, use the HtmlCssToImage.DependencyInjection package for seamless integration:
dotnet add package HtmlCssToImage.DependencyInjectionConfigure in Program.cs:
builder.Services.AddHtmlCssToImage(options =>{ options.UserId = "your-user-id"; options.ApiKey = "your-api-key";});Inject and use in your services:
public class ImageService{ private readonly IHtmlCssToImageClient _client;
public ImageService(IHtmlCssToImageClient client) { _client = client; }
public async Task<string> GenerateImageAsync(string html, string css) { var request = new CreateHtmlCssImageRequest { Html = html, Css = css }; using var result = await _client.CreateImageAsync(request); return result.Success ? result.Response.Url : null; }}Blazor Integration
Section titled “Blazor Integration”For Blazor applications, use the HtmlCssToImage.Blazor package to easily generate Open Graph image tags:
dotnet add package HtmlCssToImage.BlazorRazor Pages / MVC Tag Helpers
Section titled “Razor Pages / MVC Tag Helpers”For ASP.NET Core Razor Pages and MVC applications, use the HtmlCssToImage.TagHelpers package:
dotnet add package HtmlCssToImage.TagHelpersBatch Image Creation
Section titled “Batch Image Creation”Create multiple images in a single API call:
var variations = new List<CreateHtmlCssImageRequest>{ new() { Html = "<div>Image 1</div>", Css = "div { color: red; }" }, new() { Html = "<div>Image 2</div>", Css = "div { color: blue; }" }, new() { Html = "<div>Image 3</div>", Css = "div { color: green; }" }};
using var result = await client.CreateImageBatchAsync(variations);
if (result.Success){ foreach (var image in result.Response) { Console.WriteLine($"Image URL: {image.Url}"); }}Signed URLs (Create and Render)
Section titled “Signed URLs (Create and Render)”Generate signed URLs without making an API call. The image is rendered on-demand when the URL is accessed:
// Generate a signed URL synchronously (no API call)var url = client.CreateAndRenderUrl(new CreateHtmlCssImageRequest{ Html = "<div>Hello!</div>", Css = "div { padding: 20px; }"});
Console.WriteLine($"Signed URL: {url}");// Use this URL in img tags - the image renders when accessedThis is useful when you have content that may never be viewed, allowing you to generate images on-demand without using image credits upfront.
Performance & Native AOT
Section titled “Performance & Native AOT”The official .NET client is built with performance in mind and fully supports Native AOT (Ahead-of-Time) compilation in .NET 9+. The client uses source-generated JSON serialization internally.
Plain HTTP Example
Section titled “Plain HTTP Example”If you prefer not to use the NuGet package, you can make HTTP requests directly:
using System.Net.Http;using System.Net.Http.Headers;using System.Text;using System.Text.Json;
using var client = new HttpClient();
// Set up Basic Authenticationvar credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("user_id:api_key"));client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
var payload = new{ html = "<div class='box'>Hello, world!</div>", css = ".box { padding: 20px; background-color: #03B875; color: white; }"};
using var content = new StringContent( JsonSerializer.Serialize(payload), Encoding.UTF8, "application/json");
using var response = await client.PostAsync("https://hcti.io/v1/image", content);var json = await response.Content.ReadAsStringAsync();
Console.WriteLine(json);// {"url":"https://hcti.io/v1/image/be4c5118-fe19-462b-a49e-48cf72697a9d"}Legacy WebClient Example
Section titled “Legacy WebClient Example”For older .NET Framework projects:
using System.Net;
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?
Talk to a human. Email support@htmlcsstoimage.com and we’ll help you get started.