### Quick Start Source: https://github.com/andrei-m-code/net-core-html-to-image/blob/master/README.md A basic example demonstrating how to convert an HTML string to an image file. ```APIDOC ## Quick Start ```csharp await using var converter = new HtmlConverter(); // Convert HTML string to image var bytes = await converter.FromHtmlStringAsync("
This is rendered with headless Chromium.
"; var bytes = await converter.FromHtmlStringAsync(html); File.WriteAllBytes("output.jpg", bytes); ``` ``` -------------------------------- ### Specify ImageFormat for HTML to Image Conversion Source: https://context7.com/andrei-m-code/net-core-html-to-image/llms.txt Use the ImageFormat enum to select the desired output format: Jpg (default, good compression), Png (lossless, transparency), or WebP (modern, efficient compression). Magic bytes are provided as examples. ```csharp using CoreHtmlToImage; await using var converter = new HtmlConverter(); var html = "Date: 2024-01-15
Hello World
", width: 800, format: ImageFormat.Png, quality: 100 ); File.WriteAllBytes("sync-html.png", htmlBytes); // FromUrl - synchronous URL capture var urlBytes = converter.FromUrl( "https://example.com", width: 1024, format: ImageFormat.Jpg, quality: 85 ); File.WriteAllBytes("sync-url.jpg", urlBytes); // Note: Sync methods block the calling thread. // Prefer async API for new code. ``` -------------------------------- ### Disposing HtmlConverter Instance (Async) Source: https://github.com/andrei-m-code/net-core-html-to-image/blob/master/README.md Demonstrates the preferred asynchronous disposal pattern for the HtmlConverter to release resources, including the headless Chromium process. ```csharp await using var converter = new HtmlConverter(); ``` -------------------------------- ### Options Reference Source: https://github.com/andrei-m-code/net-core-html-to-image/blob/master/README.md A detailed reference for all available `HtmlConverterOptions` properties. ```APIDOC ## Options Reference | Property | Type | Default | Description | |---|---|---|---| | `Width` | `int` | `1024` | Viewport width in pixels | | `Height` | `int?` | `null` (auto) | Viewport height in pixels. When null, height is determined automatically | | `Format` | `ImageFormat` | `Jpg` | Output format: `Jpg`, `Png`, or `WebP` | | `Quality` | `int` | `100` | Image quality from 1 to 100. Applies to Jpg only | | `FullPage` | `bool` | `false` | When true, captures the full scrollable page instead of just the viewport | | `OmitBackground` | `bool` | `false` | When true, hides the default white background for transparent PNG/WebP output | | `NavigationTimeout` | `int` | `30000` | Maximum time in milliseconds to wait for page navigation. Set to 0 for no timeout | ``` -------------------------------- ### FromUrlAsync Method Source: https://context7.com/andrei-m-code/net-core-html-to-image/llms.txt Asynchronously captures a screenshot of a web page from a URL with various configuration options. ```APIDOC ## FromUrlAsync Method Asynchronously captures a screenshot of a web page from a URL. Waits for network idle before capturing to ensure all resources are loaded. ### Method ```csharp // Basic URL screenshot var bytes = await converter.FromUrlAsync("https://example.com"); File.WriteAllBytes("example.jpg", bytes); // Full-page screenshot with custom options var options = new HtmlConverterOptions { Width = 1280, Height = 800, Format = ImageFormat.Png, FullPage = true, NavigationTimeout = 60000 // 60 second timeout for slow pages }; var fullPageBytes = await converter.FromUrlAsync("https://github.com", options); File.WriteAllBytes("github-fullpage.png", fullPageBytes); // With cancellation support using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); try { var bytes2 = await converter.FromUrlAsync("https://example.com", options, cts.Token); File.WriteAllBytes("with-cancellation.png", bytes2); } catch (OperationCanceledException) { Console.WriteLine("Screenshot capture was cancelled"); } ``` ### Parameters #### Path Parameters - **url** (string) - Required - The URL of the web page to capture. #### Query Parameters - **options** (HtmlConverterOptions) - Optional - Configuration options for the screenshot. - **cancellationToken** (CancellationToken) - Optional - Token to cancel the operation. ``` -------------------------------- ### Synchronous API Usage (v1.x compatible) Source: https://github.com/andrei-m-code/net-core-html-to-image/blob/master/README.md Illustrates the use of the synchronous API for HTML to image conversion, which is compatible with older versions but blocks the calling thread. ```csharp var converter = new HtmlConverter(); // Same signatures as v1.x var bytes = converter.FromHtmlString("Hello
"); var bytes = converter.FromUrl("https://example.com", width: 800, format: ImageFormat.Png, quality: 90); ``` -------------------------------- ### Cancellation Support Source: https://github.com/andrei-m-code/net-core-html-to-image/blob/master/README.md Shows how to use `CancellationToken` to cancel asynchronous conversion operations. ```APIDOC ## Cancellation Support Async methods accept a `CancellationToken`: ```csharp using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); var bytes = await converter.FromUrlAsync("https://example.com", options, cts.Token); ``` ``` -------------------------------- ### Sync API Source: https://github.com/andrei-m-code/net-core-html-to-image/blob/master/README.md Details on the synchronous API, which is backward-compatible with v1.x, and a note on its performance implications. ```APIDOC ## Sync API (backward-compatible with v1.x) The synchronous API from v1.x is still available with the same method signatures: ```csharp var converter = new HtmlConverter(); // Same signatures as v1.x var bytes = converter.FromHtmlString("Hello
"); var bytes = converter.FromUrl("https://example.com", width: 800, format: ImageFormat.Png, quality: 90); ``` > **Note:** The async API is recommended for new code. The sync methods block the calling thread while waiting for Chromium. ``` -------------------------------- ### Disposing HtmlConverter Instance (Sync) Source: https://github.com/andrei-m-code/net-core-html-to-image/blob/master/README.md Shows the synchronous disposal pattern for the HtmlConverter. It's recommended to use the async pattern for new code. ```csharp using var converter = new HtmlConverter(); ``` -------------------------------- ### Synchronous Conversion Methods Source: https://context7.com/andrei-m-code/net-core-html-to-image/llms.txt Legacy synchronous methods for converting HTML or URLs to images, provided for v1.x compatibility. ```APIDOC ## POST /HtmlConverter/FromHtmlString ## POST /HtmlConverter/FromUrl ### Description Synchronous versions of the conversion methods. Note that these block the calling thread. ### Parameters #### Request Body - **html/url** (string) - Required - The source content. - **width** (int) - Optional - Viewport width. - **format** (ImageFormat) - Optional - Output format (Jpg, Png, WebP). - **quality** (int) - Optional - Image quality (1-100). ``` -------------------------------- ### Disposal Source: https://github.com/andrei-m-code/net-core-html-to-image/blob/master/README.md Guidance on properly disposing the `HtmlConverter` instance to release resources, including the headless Chromium process. ```APIDOC ## Disposal `HtmlConverter` manages a headless Chromium browser process internally. Always dispose it when done: ```csharp // Async (preferred) await using var converter = new HtmlConverter(); // Sync using var converter = new HtmlConverter(); ``` The browser instance is reused across multiple conversions for performance. A single `HtmlConverter` instance is safe to use from multiple threads concurrently. ``` -------------------------------- ### Capture URL Screenshots with Advanced Options Source: https://context7.com/andrei-m-code/net-core-html-to-image/llms.txt Captures screenshots from URLs using custom options like full-page rendering, timeouts, and cancellation tokens. ```csharp using CoreHtmlToImage; await using var converter = new HtmlConverter(); // Basic URL screenshot var bytes = await converter.FromUrlAsync("https://example.com"); File.WriteAllBytes("example.jpg", bytes); // Full-page screenshot with custom options var options = new HtmlConverterOptions { Width = 1280, Height = 800, Format = ImageFormat.Png, FullPage = true, NavigationTimeout = 60000 // 60 second timeout for slow pages }; var fullPageBytes = await converter.FromUrlAsync("https://github.com", options); File.WriteAllBytes("github-fullpage.png", fullPageBytes); // With cancellation support using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)); try { var bytes2 = await converter.FromUrlAsync("https://example.com", options, cts.Token); File.WriteAllBytes("with-cancellation.png", bytes2); } catch (OperationCanceledException) { Console.WriteLine("Screenshot capture was cancelled"); } ``` -------------------------------- ### Configure HtmlConverterOptions for Image Output Source: https://context7.com/andrei-m-code/net-core-html-to-image/llms.txt Customize image generation with HtmlConverterOptions. Set viewport dimensions, output format (PNG, WebP, JPG), transparency, quality, and capture behavior like full-page or navigation timeouts. ```csharp using CoreHtmlToImage; await using var converter = new HtmlConverter(); var html = "Date: 2024-01-15
This is rendered with headless Chromium.
"; var jpgBytes = await converter.FromHtmlStringAsync(html); File.WriteAllBytes("output.jpg", jpgBytes); // Result: JPEG image bytes (magic bytes: 0xFF 0xD8) // Convert a URL to image var urlBytes = await converter.FromUrlAsync("https://example.com"); File.WriteAllBytes("screenshot.jpg", urlBytes); ``` -------------------------------- ### Convert URL to Image Source: https://github.com/andrei-m-code/net-core-html-to-image/blob/master/README.md Converts the content of a given URL to image bytes. The HtmlConverter instance should be disposed after the operation. ```csharp await using var converter = new HtmlConverter(); var bytes = await converter.FromUrlAsync("https://example.com"); File.WriteAllBytes("screenshot.jpg", bytes); ``` -------------------------------- ### CoreHtmlToImage Usage Source: https://context7.com/andrei-m-code/net-core-html-to-image/llms.txt Basic usage of the HtmlConverter class to convert HTML strings and URLs to image bytes. ```APIDOC ## CoreHtmlToImage Usage This section demonstrates the basic instantiation and usage of the `HtmlConverter` class. ### Method ```csharp // Create converter instance (downloads Chromium on first use if needed) await using var converter = new HtmlConverter(); // Convert HTML string to JPG (default format) var html = @"This is rendered with headless Chromium.
"; var jpgBytes = await converter.FromHtmlStringAsync(html); File.WriteAllBytes("output.jpg", jpgBytes); // Convert a URL to image var urlBytes = await converter.FromUrlAsync("https://example.com"); File.WriteAllBytes("screenshot.jpg", urlBytes); ``` ``` -------------------------------- ### Convert HTML String to Image Source: https://github.com/andrei-m-code/net-core-html-to-image/blob/master/README.md Converts a given HTML string to image bytes. Ensure the HtmlConverter is properly disposed after use. ```csharp await using var converter = new HtmlConverter(); var html = @"This is rendered with headless Chromium.
"; var bytes = await converter.FromHtmlStringAsync(html); File.WriteAllBytes("output.jpg", bytes); ``` -------------------------------- ### HtmlConverter.FromHtmlStringAsync Source: https://context7.com/andrei-m-code/net-core-html-to-image/llms.txt Converts an HTML string into an image byte array asynchronously using specified conversion options. ```APIDOC ## POST /HtmlConverter/FromHtmlStringAsync ### Description Converts a provided HTML string into an image format based on the provided HtmlConverterOptions. ### Parameters #### Request Body - **html** (string) - Required - The HTML content to convert. - **options** (HtmlConverterOptions) - Optional - Configuration object containing Width, Height, Format, Quality, OmitBackground, FullPage, and NavigationTimeout. ### Response #### Success Response (200) - **bytes** (byte[]) - The resulting image data. ``` -------------------------------- ### Handle HtmlConversionException During Conversion Source: https://context7.com/andrei-m-code/net-core-html-to-image/llms.txt Catch HtmlConversionException for conversion failures, accessing the InnerException for underlying PuppeteerSharp errors. Also handles ArgumentException for invalid inputs and ObjectDisposedException if the converter is used after disposal. ```csharp using CoreHtmlToImage; await using var converter = new HtmlConverter(); // Handle conversion errors try { var bytes = await converter.FromUrlAsync("https://this-domain-does-not-exist.invalid"); } catch (HtmlConversionException ex) { Console.WriteLine($"Conversion failed: {ex.Message}"); // Output: "Conversion failed: Failed to convert URL 'https://...' to image: ..." if (ex.InnerException != null) { Console.WriteLine($"Underlying error: {ex.InnerException.Message}"); // Contains PuppeteerSharp exception details } } // Handle argument validation errors try { var bytes = await converter.FromHtmlStringAsync(""); } catch (ArgumentException ex) { Console.WriteLine($"Invalid argument: {ex.Message}"); // Output: "Invalid argument: html (Parameter 'html')" } // Handle disposed converter var disposedConverter = new HtmlConverter(); await disposedConverter.DisposeAsync(); try { var bytes = await disposedConverter.FromHtmlStringAsync("Test
"); } catch (ObjectDisposedException ex) { Console.WriteLine("Converter has been disposed"); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.