Convert HTML to JPG in C#

To convert HTML to JPG in C#, use Converter.ConvertHTML() with an HTML source, ImageSaveOptions, ImageFormat.Jpeg, and an output path or stream provider. ImageSaveOptions lets you control page setup, background color, resolution, CSS media type, and rendering quality.

This guide shows the core HTML to JPG workflows for C# applications: a one-line conversion, a file-based conversion, custom image rendering options, and output stream providers. Before running the examples, install and configure Aspose.HTML for .NET in your project. The complete C# example project is available in the Aspose.HTML for .NET GitHub repository.

JPG is a lossy raster image format commonly used for web publishing, previews, reports, email attachments, and slide decks. Use HTML to JPG conversion when your application needs to render a web page, generated HTML, or template output into a compact image file.

Convert HTML to JPG with One Line of C#

For simple input, the static Converter class provides a short path from HTML content to a JPG image. The example below passes HTML code, a base URI, ImageSaveOptions configured for JPEG output, and the output file path to ConvertHTML().

1// Convert HTML to JPG in C#
2
3// Invoke the ConvertHTML() method to convert the HTML code to JPG image
4Converter.ConvertHTML(@"<h1>Convert HTML to JPG!</h1>", ".", new ImageSaveOptions(ImageFormat.Jpeg), Path.Combine(OutputDir, "convert-with-single-line.jpg"));

Use this pattern when your HTML is already available as a string and basic JPG rendering settings are enough. For file input, page setup, resolution, or custom output streams, use the workflows below.

Convert an HTML File to JPG in C#

A typical file-based conversion uses an HTMLDocument instance, an ImageSaveOptions object configured with ImageFormat.Jpeg, and Converter.ConvertHTML(). By default, ImageSaveOptions uses PNG as the output image format, so set the format explicitly for JPG output.

The workflow is:

  1. Load the HTML file with an HTMLDocument constructor.
  2. Create an ImageSaveOptions instance with ImageFormat.Jpeg.
  3. Call Converter.ConvertHTML(document, options, savePath) to render the HTML document as a JPG image.

The next example uses the sample file spring.html. The following C# code converts spring.html to spring-output.jpg.

 1// Convert HTML to JPG using C#
 2
 3// Prepare a path to a source HTML file
 4string documentPath = Path.Combine(DataDir, "spring.html");
 5
 6// Prepare a path to save the converted file 
 7string savePath = Path.Combine(OutputDir, "spring-output.jpg");
 8
 9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Create an instance of the ImageSaveOptions class 
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg);
14
15// Convert HTML to JPG
16Converter.ConvertHTML(document, options, savePath);

After conversion, the JPG output preserves the visual layout of the source HTML:

HTML to JPG conversion result for the spring.html sample

When to Use HTML to JPG Conversion

Use JPG output when you need compact raster previews of HTML pages, thumbnails for a web UI, or shareable image snapshots where small file size matters more than transparency. JPG is usually a poor fit for pages with crisp text, diagrams, transparent backgrounds, or UI details that should remain lossless; for those cases, consider HTML to PNG or HTML to PDF.

For JPG output, create ImageSaveOptions(ImageFormat.Jpeg) explicitly because the default ImageSaveOptions.Format value is PNG. You can then tune page setup, background color, resolution, antialiasing, and stream-based output.

Customize HTML to JPG with ImageSaveOptions

Use ImageSaveOptions when the output image needs custom rendering settings. The same options class is used for PNG, JPG, BMP, GIF, and TIFF output; for JPG, set Format to ImageFormat.Jpeg.

OptionUse it to control
FormatOutput image format, such as PNG, JPG, BMP, GIF, or TIFF. The default value is ImageFormat.Png.
PageSetupOutput page size and margins used during rendering.
BackgroundColorBackground fill color. The default value is transparent.
HorizontalResolutionHorizontal resolution for output images in pixels per inch. The default value is 300 dpi.
VerticalResolutionVertical resolution for output images in pixels per inch. The default value is 300 dpi.
UseAntialiasingRendering quality for shapes, text, and images. Antialiasing is enabled by default.
CSSCSS media type and related CSS processing options.
TextText rendering options for image output.

The following example creates a JPG file with custom image save options such as page size and background color.

 1// Convert HTML to JPG in C# with custom settings
 2
 3string documentPath = Path.Combine(OutputDir, "save-options.html");
 4string savePath = Path.Combine(OutputDir, "save-options-output.jpg");
 5
 6// Prepare HTML code and save it to a file
 7string code = "<h1> Image SaveOptions </h1>\r\n" +
 8              "<p>Using ImageSaveOptions Class, you can programmatically apply a wide range of conversion parameters such as BackgroundColor, Format, Compression, PageSetup, etc.</p>\r\n";
 9
10File.WriteAllText(documentPath, code);
11
12// Initialize an HTML Document from the html file
13using HTMLDocument document = new HTMLDocument(documentPath);
14
15// Set up the page-size 500x250 pixels, margins and change the background color to AntiqueWhite
16ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
17{
18    BackgroundColor = System.Drawing.Color.AntiqueWhite
19};
20options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 250), new Margin(40, 40, 20, 20));
21
22// Convert HTML to JPG
23Converter.ConvertHTML(document, options, savePath);

The next example converts an HTML file to JPG using custom page setup, background color, resolution, and antialiasing settings.

 1// Convert HTML to JPG in C# with with custom background, resolution, and antialiasing settings
 2
 3// Prepare a path to a source HTML file
 4string documentPath = Path.Combine(DataDir, "color.html");
 5
 6// Prepare a path to save the converted file 
 7string savePath = Path.Combine(OutputDir, "color-output-options.jpg");
 8
 9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Initialize ImageSaveOptions 
13ImageSaveOptions options = new ImageSaveOptions(ImageFormat.Jpeg)
14{
15    UseAntialiasing = true,
16    HorizontalResolution = 200,
17    VerticalResolution = 200,
18    BackgroundColor = System.Drawing.Color.AliceBlue
19};
20options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 500), new Margin(30, 20, 10, 10));
21
22// Convert HTML to JPG
23Converter.ConvertHTML(document, options, savePath);

In this example, ImageSaveOptions is passed to Converter.ConvertHTML() together with the loaded HTML document and output path. The sample configures background color, horizontal and vertical resolution, antialiasing behavior, page size, and margins.

Use UseAntialiasing = true when visual smoothness is more important than rendering speed. Use UseAntialiasing = false for simpler, performance-oriented rendering where sharper edges or faster processing are acceptable.

The custom settings example produces a JPG image similar to the following output:

HTML to JPG conversion result with custom image save options

Save HTML to JPG with an Output Stream Provider

Image formats can produce multiple output files, especially when rendered content spans more than one page. If your application needs to save generated JPG files to custom storage, such as memory, a database, cloud storage, or an archive, implement ICreateStreamProvider.

The ICreateStreamProvider interface lets your code create a stream at the beginning of document or page rendering and release it after rendering is complete.

The example below implements a custom MemoryStreamProvider.

 1// Implement a custom MemoryStream provider for advanced control over HTML rendering output streams
 2
 3class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
 4{
 5    // List of MemoryStream objects created during the document rendering
 6    public List<MemoryStream> Streams { get; } = new List<MemoryStream>();
 7
 8    public Stream GetStream(string name, string extension)
 9    {
10        // This method is called when only one output stream is required, for instance for XPS, PDF or TIFF formats
11        MemoryStream result = new MemoryStream();
12        Streams.Add(result);
13        return result;
14    }
15
16    public Stream GetStream(string name, string extension, int page)
17    {
18        // This method is called when the creation of multiple output streams are required. For instance, during the rendering HTML to list of image files (JPG, PNG, etc.)
19        MemoryStream result = new MemoryStream();
20        Streams.Add(result);
21        return result;
22    }
23
24    public void ReleaseStream(Stream stream)
25    {
26        // Here you can release the stream filled with data and, for instance, flush it to the hard-drive
27    }
28
29    public void Dispose()
30    {
31        // Releasing resources
32        foreach (MemoryStream stream in Streams)
33            stream.Dispose();
34    }
35}

The following C# code uses MemoryStreamProvider during HTML to JPG conversion and saves the generated stream content to a file.

 1// Convert HTML to JPG in C# using memory stream
 2
 3// Create an instance of MemoryStreamProvider
 4using MemoryStreamProvider streamProvider = new MemoryStreamProvider();
 5
 6// Initialize an HTML document
 7using HTMLDocument document = new HTMLDocument(@"<h1>Convert HTML to JPG File Format!</h1>", ".");
 8
 9// Convert HTML to JPG using the MemoryStreamProvider
10Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
11
12// Get access to the memory stream that contains the result data
13MemoryStream memory = streamProvider.Streams.First();
14memory.Seek(0, SeekOrigin.Begin);
15
16// Flush the result data to the output file
17using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.jpg")))
18{
19    memory.CopyTo(fs);
20}

Related Image Workflows

Related HTML Conversion Guides

Other Platforms

Try Online HTML to JPG Conversion

                
            

Use the online HTML to JPG Converter for quick manual conversion. Use Aspose.HTML for .NET when HTML to JPG rendering must run inside a C# application, service, reporting tool, or image-generation workflow.

HTML to JPG Converter