C#/VB.NET: Convert HTML to Images

A common use of HTML is to display data and information on websites, web applications, and a variety of platforms. It is sometimes necessary to convert HTML to images like JPG, PNG, TIFF, BMP etc. since images are difficult to modify and can be accessed by virtually anyone. This article will show you how to perform the HTML to images conversion programmatically in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert HTML to Images

Spire.Doc for .NET offers the Document.SaveToImages() method to convert HTML to Images. Here are detailed steps.

  • Create a Document instance.
  • Load an HTML sample document using Document.LoadFromFile() method.
  • Save the document as an image using Document.SaveToImages() method.
  • C#
  • VB.NET
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing.Imaging;

namespace HTMLToImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document mydoc = new Document();
           
            //Load an HTML sample document
            mydoc.LoadFromFile(@"sample.html", FileFormat.Html, XHTMLValidationType.None);
           
            //Save to image. You can convert HTML to BMP, JPEG, PNG, GIF, Tiff etc
            Image image = mydoc.SaveToImages(0, ImageType.Bitmap);
            image.Save("HTMLToImage.png", ImageFormat.Png);
        }
    }
}

C#/VB.NET: Convert HTML to Images

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.