C# Convert HTML to JPG, PNG, BMP and GIF Image with .NET

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, we will be learning how to convert HTML to image using C#. Let us explore the following uses cases:

First of all, let us check the basic HTML file, containing some text and image, 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 using C#

You can convert HTML to JPG image using 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 for converting HTML to JPG image:

  1. Initialize image rendering options using ImageSaveOptions class
  2. Set page size or background-color
  3. Convert the HTML file

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

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

HTML to JPG

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.

Convert HTML to PNG Image using 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 PNG image because it is a popular and compatible file format.

You can follow the steps below for the conversion:

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

Following code snippet shows how to convert HTML file to PNG image using C#:

Convert HTML to BMP Image using C#

You can convert HTML to BMP image using 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 using C#

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

HTML to GIF image conversion 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

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

Conclusion

To sum up, we have learned how to convert HTML to JPG, PNG, BMP and GIF in .NET applications using C# or VB.NET. 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 a 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

Convert EPUB to PDF