Convert HTML to JPG in C#

Are you looking for a way to convert HTML webpages or documents into JPG using C#? If you are a developer who has to convert HTML pages to JPG or you have to generate HTML string output as JPG image, this article will help you learn how to do it with C# and build your own HTML to JPG converter application. You will find detailed steps and code snippets that can help you convert HTML to JPG in C# very easily.

We create web pages using HTML (Hyper Text Markup Language), a markup language. HTML pages contain HTML elements such as forms, text, images, animations, links, etc. We can convert and save web pages as JPG images programmatically. JPG is the most widely used image format. High-quality images can be stored in JPG files with lossless compression. It is the most popular image format for sharing photos and other images over the internet and between mobile devices and PCs.

Converting HTML to JPG allows you to save websites or other standalone HTML web pages in a portable image format. The converted images can then be used for various purposes, such as embedding in a PowerPoint presentation or sending through e-mail or sharing on the internet. So, keep reading for detailed steps on how to convert HTML to JPG in C#.

The following topics shall be covered in this article:

  1. C# HTML to JPG Converter API
  2. Convert HTML to JPG
  3. Convert HTML to JPG with Save Options
  4. Export HTML String to JPG
  5. Convert HTML to JPG from Live URL

C# HTML to JPG Converter API

For converting HTML to JPG, we will be using the Aspose.HTML for .NET API. The API allows you to create HTML documents. Additionally, it allows you to modify and extract data from HTML documents. The API also enables you to convert and render HTML documents without the need for third-party software.

The HTMLDocument class of the API represents an HTML document or the webpage that we see in the browser. We can specify various save options while saving as a JPG image using the ImageSaveOptions class. The API provides the conversion of the supported formats using the Converter class. It provides a wide range of conversions to the popular formats, such as PDF, XPS, image formats, etc. The ConvertHTML() methods of this class allows converting HTML to other formats.

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

PM> Install-Package Aspose.Html

Convert HTML to JPG C# API

We can easily convert any HTML file to a JPG image by following the steps given below:

  1. Firstly, load an HTML document using the HTMLDocument class.
  2. Next, create an instance of the ImageSaveOptions class.
  3. After that, specify the format as ImageFormat.Jpeg.
  4. Finally, call the Converter.ConvertHTML() method. It takes HTMLDocument, ImageSaveOptions, and output JPG file path as arguments.

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

Convert HTML to JPG using C#

Convert HTML to JPG using C#

C# HTML to JPG - Customization

The ImageSaveOptions class offers multiple properties for easy conversion of HTML to Image formats. We can use the following properties:

  • The SmoothingMode property lets you choose the image’s rendering quality. We can select any of the available values such as Invalid, Default, HighSpeed, HighQuality, None, and AntiAlias to render images.
  • The BackgroundColor property is for setting the background color of the image. The default background color is transparent.
  • HorizontalResolution and VerticalResolution properties allows setting the horizontal and vertical resolution for output images in pixels per inch. 300 dpi is the default value for these properties.
  • The PageSetup property specifies the page size and margins in pixels.

We can set above-mentioned save options for saving HTML file as a JPG image by following the steps given below:

  1. Firstly, load an HTML document using the HTMLDocument class.
  2. Next, create an instance of the ImageSaveOptions class.
  3. After that, specify save options such as HorizontalResolution, VerticalResolution, BackgroundColor, etc.
  4. Finally, call the Converter.ConvertHTML() method to convert an HTML file into a JPG.

The following code sample shows how to convert an HTML file to a JPG with additional options in C#.

Convert HTML to JPG with Save Options in C#

Convert HTML to JPG with Save Options in C#

Export HTML String to JPG in C#

We can also convert an HTML string to a JPG image by following the steps given below:

  1. Firstly, define an HTML string.
  2. Next, create an instance of the ImageSaveOptions class.
  3. Optionally, define save options.
  4. Finally, call the Converter.ConvertHTML() method to convert an HTML string to a JPG. It takes HTML string, ImageSaveOptions and output JPG file path as arguments.

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

Export HTML String to JPG in C#

Export HTML String to JPG in C#

Convert HTML to JPG from Live URL in C#

We can also save any webpage from a live URL as a JPG document by following the steps given below:

  1. Firstly, create an instance of the Url class with the URL of a webpage.
  2. Next, create an instance of the ImageSaveOptions class.
  3. Optionally, define image save options.
  4. Finally, save the HTML as a JPG using the Converter.ConvertHTML() method. It takes the Url, ImageSaveOptions, and output JPG file path as arguments.

The following code sample shows how to convert HTML to JPG from Live URL using C#.

Convert HTML to JPG from Live URL in C#

Convert HTML to JPG from Live URL in C#

Get Free License

You can get a free temporary license to try Aspose.HTML for .NET without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • load an HTML file;
  • specify JPG save options;
  • convert HTML from a file, string or a live webpage to JPG in C#.

Besides converting HTML to JPG in C#, you can learn more about Aspose.HTML for .NET API using documentation. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also