Convert HTML to JPG | C#

JPG files can contain high-quality image data with lossless compression. This unique compression feature allows to quickly and efficiently share JPG images and use them widely on the Web, computers, and mobile devices. Converting HTML files to the JPG image may be required, for example, if you want to add a web page in a PowerPoint presentation or send it by e-mail. Using Converter.ConvertHTML methods is the most common way to convert HTML code into various formats. JPG is a commonly used format for storing images, and HTML to JPG conversion is very popular. With Aspose.HTML, you can convert HTML to JPG format programmatically with full control over a wide range of conversion parameters.

In this article, you find information on converting HTML to JPEG by using ConvertHTML() methods of the Converter class and applying ImageSaveOptions and ICreateStreamProvider parameters.

Text “Banner HTML to JPG Converter”

HTML to JPG by a single line of code

The static methods of the Converter class are primarily used as the easiest way to convert HTML code into various formats. You can convert HTML to JPG in your C# application literally with a single line of code!

using System.IO;
using Aspose.Html.Converters;
using Aspose.Html.Rendering.Image;
using Aspose.Html.Saving;
...
    // Invoke the ConvertHTML method to convert the HTML code to JPG image           
    Converter.ConvertHTML(@"<h1>Convert HTML to JPG!</h1>", ".", new ImageSaveOptions(ImageFormat.Jpeg), Path.Combine(OutputDir, "convert-with-single-line.jpg"));

Convert HTML to JPG

Converting a file to another format using the ConvertHTML() method is a sequence of operations among which document loading and saving:

  1. Load an HTML file using the HTMLDocument class (spring.html).
  2. Create a new ImageSaveOptions object with JPG ImageFormat. By default, the Format property is PNG.
  3. Use the ConvertHTML() method of the Converter class to save HTML as a JPG image. You need to pass the HTMLDocument, ImageSaveOptions, and output file path to the ConvertHTML() method to convert HTML to JPG.

Please take a look over the following C# code snippet which shows the process of converting HTML to JPG using Aspose.HTML for .NET.

using System.IO;
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Rendering.Image;
using Aspose.Html.Saving;
...
    // Prepare a path to a source HTML file
    string documentPath = Path.Combine(DataDir, "spring.html");

    // Prepare a path for converted file saving 
    string savePath = Path.Combine(OutputDir, "spring-output.jpg");
    
    // Initialize an HTML document from the file
    using var document = new HTMLDocument(documentPath);
    
    // Initialize ImageSaveOptions 
    var options = new ImageSaveOptions(ImageFormat.Jpeg);
    
    // Convert HTML to JPG
    Converter.ConvertHTML(document, options, savePath);

The figure illustrates the spring-output.jpg file.

Text “spring-output.jpg image”

Save Options

Aspose.HTML allows converting HTML to JPG using default or custom save options. ImageSaveOptions usage enables you to customize the rendering process. You can specify the image format, page size, margins, CSS media-type, etc.

PropertyDescription
CompressionSets Tagged Image File Format (TIFF) Compression. By default, this property is LZW.
CSSGets a CssOptions object which is used for configuration of CSS properties processing.
FormatSets the ImageFormat (JPG, PNG, BMP, TIFF, or GIF). By default, this property is PNG.
BackgroundColorThis property sets the color that will fill the background. By default, this property is Transparent.
PageSetupThis property gets a page setup object and uses it for configuration output page-set.
HorizontalResolutionSets horizontal resolution for output images in pixels per inch. The default value is 300 dpi.
VerticalResolutionSets vertical resolution for output images in pixels per inch. The default value is 300 dpi.
SmoothingModeThis property sets the rendering quality for this image. Available values are Invalid, Default, HighSpeed, HighQuality, None, and AntiAlias.
TextGets a TextOptions object which is used for configuration of text rendering.

The following example shows how to use ImageSaveOptions and create the output image with custom save options such as a page size and background color:

using System.IO;
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Rendering.Image;
using Aspose.Html.Saving;
using System.Drawing;
using Aspose.Html.Drawing;
...
    string documentPath = Path.Combine(OutputDir, "save-options.html");
    string savePath = Path.Combine(OutputDir, "save-options-output.jpg");

    // Prepare HTML code and save it to a file
    var code = "<h1>  Image SaveOptions </h1>\r\n" +
               "<p>Using ImageSaveOptions Class, you can programmatically apply a wide range of conversion parameters such as BackgroundColor, Format, Compression, PageSetup, etc.</p>\r\n";
    
    File.WriteAllText(documentPath, code);
    
    // Initialize an HTML Document from the html file
    using var document = new HTMLDocument(documentPath);
    
    // Set up the page-size 400x250 pixels, margins and change the background color to AntiqueWhite
    var options = new ImageSaveOptions(ImageFormat.Jpeg)
    {
        BackgroundColor = Color.AntiqueWhite                    
    };
    options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(400, 250), new Margin(40, 40, 20, 20));                                     
    
    // Convert HTML to JPG
    Converter.ConvertHTML(document, options, savePath);

Convert HTML to JPG using ImageSaveOptions

To convert HTML to JPG with ImageSaveOptions specifying, you should follow a few steps:

  1. Load an HTML file using one of the HTMLDocument() constructors of the HTMLDocument class (color.html).
  2. Create a new ImageSaveOptions object with JPG ImageFormat and specify save options. By default, the Format property is PNG.
  3. Use the ConvertHTML() method of the Converter class to save HTML as a JPG image. You need to pass the HTMLDocument, ImageSaveOptions, and output file path to the ConvertHTML() method to convert HTML to JPG.

The following C# code snippet shows how to convert HTML to JPG using custom save options:

using System.IO;
using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Rendering.Image;
using Aspose.Html.Saving;
using System.Drawing;
using Aspose.Html.Drawing;
using System.Drawing.Drawing2D;
...
    // Prepare a path to a source HTML file
    string documentPath = Path.Combine(DataDir, "color.html");

    // Prepare a path for converted file saving 
    string savePath = Path.Combine(OutputDir, "color-output-options.jpg");
    
    // Initialize an HTML document from the file
    using var document = new HTMLDocument(documentPath);
    
    // Initialize ImageSaveOptions 
    var options = new ImageSaveOptions(ImageFormat.Jpeg)
    {
        SmoothingMode = SmoothingMode.HighQuality,
        HorizontalResolution = 200,
        VerticalResolution = 200,
        BackgroundColor = Color.AliceBlue
    };
    options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(500, 500), new Margin(30, 20, 10, 10));
    
    // Convert HTML to JPG
    Converter.ConvertHTML(document, options, savePath)

The ImageSaveOptions() constructor initializes an instance of the ImageSaveOptions class that is passed to ConvertHTML() method. The ConvertHTML() method takes the document, options, output file path savePath and performs the conversion operation.

In the above example, we use:

  • BackgroundColor property that sets the color that will fill the background. The default BackgroundColor is Transparent;
  • HorizontalResolution and VerticalResolution properties that set horizontal/vertical resolution for output images in pixels per inch. By default, these properties are 300 dpi;
  • SmoothingMode property that sets the rendering quality for this image;
  • PageSetup property that specifies the page size and margins in pixels.

The ImageSaveOptions class provides numerous properties that give you full control over a wide range of parameters and improve the process of converting HTML to Image formats. Among these properties, SmoothingMode that enables you to set the rendering quality for the image. Available values are Invalid, Default, HighSpeed, HighQuality, None, and AntiAlias. You can select any value, considering the advantages and disadvantages of each one.

The figure illustrates the color-output-options.jpg file.

Text ““Color” JPG image”

Output Stream Providers

If it is required to save files in the remote storage (e.g., cloud, database, etc.) you can implement the ICreateStreamProvider interface to have manual control over the file creating process. This interface is designed as a callback object to create a stream at the beginning of the document/page (depending on the output format) and release the early created stream after rendering the document/page.

The example below shows how to implement and use your own MemoryStreamProvider in the application:

using System.IO;
using System.Collections.Generic;
...
    class MemoryStreamProvider : Aspose.Html.IO.ICreateStreamProvider
    {
        // List of MemoryStream objects created during the document rendering
        public List<MemoryStream> Streams { get; } = new List<MemoryStream>();

        public Stream GetStream(string name, string extension)
        {
            // This method is called when the only one output stream is required, for instance for XPS, PDF or TIFF formats.
            MemoryStream result = new MemoryStream();
            Streams.Add(result);
            return result;
        }
    
        public Stream GetStream(string name, string extension, int page)
        {
            // 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.)
            MemoryStream result = new MemoryStream();
            Streams.Add(result);
            return result;
        }
    
        public void ReleaseStream(Stream stream)
        {
            //  Here you can release the stream filled with data and, for instance, flush it to the hard-drive
        }
    
        public void Dispose()
        {
            // Releasing resources
            foreach (var stream in Streams)
                stream.Dispose();
        }
    }

using System.IO;
using Aspose.Html;
using System.Linq;
using Aspose.Html.Converters;
using Aspose.Html.Rendering.Image;
using Aspose.Html.Saving;
...
    // Create an instance of MemoryStreamProvider
    using var streamProvider = new MemoryStreamProvider();

    // Initialize an HTML document
    using var document = new HTMLDocument(@"<h1>Convert HTML to JPG File Format!</h1>", ".");
    
    // Convert HTML to JPG using the MemoryStreamProvider
    Converter.ConvertHTML(document, new ImageSaveOptions(ImageFormat.Jpeg), streamProvider);
    
    // Get access to the memory stream that contains the result data
    var memory = streamProvider.Streams.First();
    memory.Seek(0, SeekOrigin.Begin);
    
    // Flush the result data to the output file
    using (FileStream fs = File.Create(Path.Combine(OutputDir, "stream-provider.jpg")))
    {
        memory.CopyTo(fs);
    }