Programmatic HTML to JPEG Conversion for .NET
Aspose.HTML for .NET renders HTML documents and web pages as JPEG 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 JPEG Converter
Use the interactive example below to upload an HTML file or provide a URL, render it as JPEG, and review the corresponding C# code. The example selects JPEG output explicitly.
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 JPEG in C#
- Load the source with an HTMLDocument constructor.
- Create
ImageSaveOptionswithImageFormat.Jpeg. - Configure page setup, resolution, background color, or rendering options.
- Call Converter.ConvertHTML() with the document, options, and output destination.
HTML to JPEG Conversion Options and Documentation
Read the
HTML to JPEG 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 JPEG Conversion Notes
- JPEG is lossy, so fine text and sharp edges can show compression artifacts.
- JPEG has no transparency; set
BackgroundColorfor a predictable background. - Configure
PageSetupfor the required image bounds. - Fonts and external assets must be available during rendering.
- Multi-page documents can produce several JPEG files.
Install Aspose.HTML for .NET
Add the package to your project, then use the C# example above as the minimal HTML to JPEG 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 JPEG Conversion FAQ
1. How do I convert HTML to JPEG in C#?
Load the source into an HTMLDocument, create ImageSaveOptions with ImageFormat.Jpeg, and call Converter.ConvertHTML() with the document and output destination. Each rendered page is written as a JPEG image.
2. Can I convert a webpage URL to JPEG?
Yes. Load the webpage URL into an HTMLDocument, then use the same JPEG conversion workflow. The page, CSS, fonts, images, and other dependencies must remain accessible during rendering.
3. Does JPEG preserve transparency?
No. JPEG has no alpha channel. Set ImageSaveOptions.BackgroundColor to control how transparent page areas are filled instead of relying on an unintended default canvas color.
4. How do I control JPEG dimensions?
Set the rendered page size through ImageSaveOptions.PageSetup, then configure HorizontalResolution and VerticalResolution for pixel density. The final JPEG dimensions depend on both page geometry and resolution.
5. Why can one page create multiple JPEG files?
Image rendering follows pagination rather than browser scrolling. If the configured page size divides long HTML content into several pages, the converter can create a separate JPEG for each page.
Other Supported HTML Conversions
Explore other HTML conversion APIs and C# examples: