Convert HTML to JPG – C# Examples and Online Converter

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 JPG by using ConvertHTML() methods of the Converter class and applying ImageSaveOptions and ICreateStreamProvider parameters.

Online HTML Converter

You can check the Aspose.HTML API functionality and convert HTML in real-time. Please load HTML from the local file system, select the output format and run the example. In the example, the save options are set by default. You will immediately receive the result as a separate file.

If you want to convert HTML to JPG programmatically, please see the following C# code examples.

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!

See also  How To Print Then Cut On Cricut – Print Then Cut vs Cut Image Cricut (Full Cricut Sticker Tutorial)

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.

The figure illustrates the spring-output.jpg file.

Text “spring-output.jpg image”

Save Options

The JPG images creation functionality can be enhanced with save options per your needs. 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.

PropertyDescriptionCompressionSets 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.

See also  200+ Best Happy Birthday Brother Wishes and Images (2023)

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:

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:

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.

See also  How does the image used in this advertisement help convey the message of the poster?

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:

Text “Banner HTML to JPG Converter”