Programmatic HTML to Image Conversion for .NET
Aspose.HTML for .NET renders HTML documents and web pages as PNG, JPEG, GIF, BMP, or TIFF images inside C# applications. Load a file, URL, string, stream, or HTMLDocument , create ImageSaveOptions , select the required image format, and call Converter.ConvertHTML() .
Use this workflow when an application needs a raster representation of rendered HTML.
C# Code Example and Interactive HTML to Image Converter
Use the interactive example below to upload an HTML file or provide a URL, render it as Image, and review the corresponding C# code. PNG is selected initially; choose another supported image format when required.
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Saving;
using HTMLDocument document = new HTMLDocument("{{input lower}}");
{{#if_output 'PDF'}}
PdfSaveOptions options = new PdfSaveOptions();
{{/if_output}}
{{#if_output 'MHTML'}}
MHTMLSaveOptions options = new MHTMLSaveOptions();
{{/if_output}}
{{#if_output 'DOCX'}}
DocSaveOptions options = new DocSaveOptions();
{{/if_output}}
{{#if_output 'XPS'}}
XpsSaveOptions options = new XpsSaveOptions();
{{/if_output}}
{{#if_output 'MD'}}
MarkdownSaveOptions options = new MarkdownSaveOptions();
{{/if_output}}
{{#if_output 'BMP' 'JPG' 'GIF' 'PNG' 'TIFF'}}
ImageSaveOptions options = new ImageSaveOptions(ImageFormat.{{output param2 camel}});
{{/if_output}}
{{#if_output 'BMP' 'JPG' 'GIF' 'PNG' 'TIFF' 'PDF' 'MHTML' 'MD' 'XPS' 'DOCX'}}
Converter.ConvertHTML(document, options, "output.{{output lower}}");
{{/if_output}}
{{#if_output 'XHTML'}}
document.Save("output.xhtml", new HTMLSaveOptions() { DocumentType = HTMLSaveOptions.XHTML });
{{/if_output}}
How to Convert HTML to Image in C#
- Load the source with an HTMLDocument constructor.
- Create
ImageSaveOptionsand select the requiredImageFormat. - Configure page setup, resolution, background color, or rendering options.
- Call Converter.ConvertHTML() with the document, options, and output destination.
HTML to Image Conversion Options and Documentation
Read the
HTML to Image documentation
and consult the ImageSaveOptions reference for rendering settings.
Use
ImageSaveOptions
to select PNG, JPEG, BMP, GIF, or TIFF output and configure page setup, background color, resolution, antialiasing, and text rendering. Image conversion can create multiple output files for a multi-page document; use
ICreateStreamProvider
when the application must control each output stream.
HTML to Image Conversion Notes
- Choose the format for the delivery requirement: PNG is lossless, JPEG is compact for photos, and GIF has a limited palette.
- Raster output does not retain links, selectable text, DOM structure, or responsive behavior.
- Viewport and
PageSetupaffect long pages and wide content. - Inline markup needs a base URI for relative resources.
- One document can create several page images.
Install Aspose.HTML for .NET
Add the package to your project, then use the C# example above as the minimal HTML to Image workflow.
Install Aspose.HTML for .NET from NuGet with the .NET CLI:
dotnet add package Aspose.HTML
You can also use the NuGet Package Manager UI or run Install-Package Aspose.HTML in the Package Manager Console. See the
installation guide
for setup details, or request a
temporary license
to evaluate the API without evaluation limitations.
HTML to Image Conversion FAQ
1. Which image formats can I create from HTML?
Use ImageSaveOptions to render HTML as PNG, JPEG, BMP, GIF, or TIFF. Choose the format according to the required transparency, compression, color detail, file size, and compatibility.
2. How do I convert HTML to an image in C#?
Load the source into an HTMLDocument, create ImageSaveOptions for the required format, and call Converter.ConvertHTML() with the document and output destination. Rendering can create one image for each configured page.
3. Which format supports transparency?
Use PNG when the rendered page needs alpha transparency and lossless edges. JPEG has no alpha channel, so transparent areas must be filled with a background color before JPEG output is used.
4. Can one HTML document create multiple images?
Yes. Image conversion follows the configured page setup, so long content can be divided into several rendered pages and written as separate image files or streams.
5. Can I convert a webpage URL directly?
Yes. Load the webpage URL into an HTMLDocument and convert that document with ImageSaveOptions. The conversion environment must be able to access the page and all required stylesheets, fonts, and images.
Other Supported HTML Conversions
Explore other HTML conversion APIs and C# examples: