How to Convert HTML to Image in C#

This tutorial briefly describes how to convert HTML to image in C# by loading the source HTML file and then saving it as a PNG image file. You can configure the output image by setting a number of properties that are explained in the sample code. Note that for writing HTML to image converter C# language is used here however you can perform the same task in many other languages as well.

Steps to Convert HTML to Image in C#

  1. Configure the project by adding Aspose.HTML for .NET from the Nuget package manager
  2. Load the desired source HTML file into HTMLDocument class object for conversion to image
  3. Initialize the ImageSaveOptions object and configure different properties of the output image
  4. Convert the HTML to image using the ConvertHTML() function from Converter class

The above steps describe how by using C# convert HTML to image such that first add the reference to the required libraries and then load the source HTML file using the HTMLDocument class. You can configure the output image by setting properties like smoothing mode, background color, horizontal, and vertical resolutions. Once all the parameters are set, convert the HTML file to image using the ConvertHTML() function that requires input file, image configurations, and the output image file name.

Code to Convert HTML to Image in C#

Here we have used the ImageSaveOptions class object that also supports setting format which is PNG by default, page setup like page layout, and CSS options to be used for rendering the HTML. Similarly, other image types which are supported include JPEG, BMP, GIF, and TIFF.

We have learned here how to convert HTML to image in C#. If you want to learn to create the HTML file from scratch before converting it to image, refer to the article on how to create HTML file in C#.

 English