HTML files are frequently used over the internet. Subsequently, Aspose.HTML for .NET API supports a lot of features to work with HTML file format. In this article, you will learn how to convert HTML to image in C#. You can render the HTML webpage to JPG, PNG, BMP, GIF and other popular image formats as covered under the following headings:

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#

You can convert HTML to JPG images in C# language in your .NET applications. The API supports several features for getting the output with different variations. For example, you can set the size of the output image as well as any background color that you want in your output image.

You need to follow the steps below to create an HTML to JPG image converter in C#:

  1. Initialize image rendering options using ImageSaveOptions class
  2. Set page size or background-color
  3. Convert the HTML to JPG in C#

Furthermore, the following code shows how to convert HTML to a JPG image in C#:

Moreover, let us 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 300dpi so the width and length of the image are 2100 because it is 7 inches in length and width.

HTML to PNG Image C# Converter

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.

You can follow the steps below for converting HTML to PNG in C#:

  1. Load input HTML file
  2. Initialize an object of ImageSaveOptions class
  3. Convert HTML to a PNG image in C#

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

C# HTML to BMP Image Conversion in C#

You can perform HTML to BMP image conversion in C# using the ImageFormat.Bmp property. Likewise, you should follow the below steps for the conversion:

  1. Load HTML file
  2. Set property with BMP ImageFormat
  3. Perform conversion with Converter class

Following code shows how to convert HTML to BMP image in C#:

Convert HTML to GIF Image in C#

HTML to GIF image conversion in C# is an important feature of the API. You can convert the file with the following steps:

  1. Load input HTML document
  2. Specify ImageSaveOptions using GIF ImageFormat
  3. Call ConvertHTML method to convert HTML to GIF image in C#

The code below shows how to convert HTML to GIF images in C# in your .NET applications:

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 converting or rendering results are with 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.