HTML files are frequently used over the internet. In certain cases, we may need to convert HTML pages into images. In this article, we will learn how to convert HTML to image in C#. We will learn to render the HTML webpage to JPG, PNG, BMP, GIF, and other popular image formats.

This article covers the following topics:

C# API to Convert HTML to Image

We will use Aspose.HTML for .NET for converting HTML files to images in C#. It allows developers to work with HTML documents in their .NET applications. It provides a wide range of features and capabilities for parsing, converting, editing, and rendering HTML documents.

Please download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Html

Converting HTML to Image - Input File

First of all, let us check the basic HTML file, containing some text and images, that we will be using as the input file in these examples.

<!DOCTYPE html>
<html>
<head>
<style>
body {
  text-align: center;
  color: black;
}
</style>
</head>
<body>
<h2>Welcome to HTML to Image C# Test Conversion</h2>
<p></p>
<img src="HTML to Image.png" alt="HTML to Image" style="width:300px">
</body>
</html>

Convert HTML to JPG Image in C#

We can convert any HTML webpage to a JPG image by following the steps below:

  1. Initialize image rendering options using the ImageSaveOptions class.
  2. Set the page size or background-color.
  3. After that, specify the ImageFormat as JPG.
  4. Finally, call the ConvertHTML() method to convert HTML to JPG.

The following code sample shows how to convert HTML to a JPG image in C#.

Moreover, let’s take a look at the generated output JPG image:

HTML to JPG Image C#

Furthermore, you can notice the background color and size of the image from the properties. The resolutions are 300 dpi so the width and length of the image are 2100 because it is 7 inches in length and width.

Convert HTML to PNG Image in C#

PNG image files are more advanced files than JPG images. For example, they can have transparent backgrounds, unlike JPG images. Therefore, sometimes it is better to convert HTML to a PNG image because it is a popular and compatible file format.

we can convert HTML to PNG by following the steps mentioned earlier. However, we just need to specify the ImageFormat.Png at step #3.

The following code snippet shows how to convert an HTML file to a PNG image in C#:

C# HTML to BMP Image Converter

Similarly, we can convert HTML to BMP by following the steps mentioned earlier. However, we just need to specify the ImageFormat.Bmp at step #3.

The following code sample shows how to convert HTML to BMP image in C#:

Convert HTML to GIF Image in C#

We can also convert HTML to GIF by following the steps mentioned earlier. However, we just need to specify the ImageFormat.Gif at step #3.

The code below shows how to convert HTML to GIF images in C#.

Get a Free License

Please get a free temporary license to try the library without evaluation limitations.

HTML to Image - Learning Resources

Besides converting HTML webpages to images, explore various other features of the library using the resources below:

Conclusion

To sum up, we have learned how to convert HTML to JPG, PNG, BMP, and GIF in C# within .NET applications. HTML conversion to these image formats is one of the most important features of Aspose.HTML for .NET API. Moreover, the conversion or rendering results have the highest fidelity and efficiency. That is why the API is the best fit for converting, editing, or manipulating HTML files. Moreover, we look forward to hearing back from you. Please feel free to contact us via the Free Support Forum in case of any assistance.

See Also

Info: Aspose Text to GIF converter allows you to create fun animations just by typing.