.NET Core C# Convert HTML to Image, URL to Image with Examples

  By : Suresh Dasari
  Posted On : 23-Apr-2023

Here we will learn how to convert html to image in c# (.NET Core) or convert HTML content/page to image in c# with examples and how to convert url to image in c# (.NET Core) with examples.

 

In c#, by using CoreHtmlToImage (.NET Core Html to Image Converter) library, we can easily convert HTML string to Image and convert URL to Image.

 

To use the CoreHtmlToImage library, first, create a console application for that Open Visual Studio à Choose Create a New Project à Select Console App à Enter the Project Name and click Next button as shown below.

 

Visual Studio Create a New .NET Core C# Project

 

After that, Select .NET Framework version and click on Create button to create the project as shown below.

 

Select .NET Framework Version to Create Project in C#

 

After creating the console application, now we will add the CoreHtmlToImage library to our project for that right click on your project à select Manage NuGet Packages à Go to the Browse tab à Search for CoreHtmlToImage à from the list select the CoreHtmlToImage package, and install it.

 

Add CoreHtmlToImage package in c# application

 

After installing the CoreHtmlToImage package, open Program.cs file and write the code as shown below.

 

using CoreHtmlToImage;
using System;
using System.IO;

namespace Tutlane
{
  class Program
  {
    static void Main(string[] args)
    {
       var html = @" <!DOCTYPE html>
                     <html>
                      <head>
                        <style>
                          table {
                            font-family: Segoe UI,Tahoma,sans-serif;
                            border-collapse: collapse;
                            width: 100%;
                          }
                          th {
                            background-color: #08c;
                            color: #fff;
                            border: 1px solid #ddd;
                            padding: 8px;
                            text-align: left;
                          }
                          td {
                           border: 1px solid #ddd;
                           padding: 8px;
                          }
                        </style>
                      </head>
                      <body>
                        <h2>Tutlane Employee Table</h2>
                        <table>
                          <tr>
                            <th>Name</th>
                            <th>Location</th>
                          </tr>
                          <tr>
                            <td>Rohini</td>
                            <td>France</td>
                          </tr>
                          <tr>
                            <td>Suresh</td>
                            <td>India</td>
                          </tr>
                          <tr>
                            <td>Trishika</td>
                            <td>Canada</td>
                          </tr>
                          <tr>
                            <td>Hanshith</td>
                            <td>USA</td>
                          </tr>
                        </table>
                      </body>
                     </html>";

       var converter = new HtmlConverter();
       var bytes = converter.FromHtmlString(html);
       File.WriteAllBytes(@"D:\Learning\Employee.jpg", bytes);
       Console.WriteLine("HTML to Image Conversion Completed");
       Console.WriteLine("Press Enter Key to Exit..");
       Console.ReadLine();
    }
  }
}

 In this example, we the added CoreHtmlToImage namespace reference in our example, using the FromHtmlString method to convert the HTML table string to image and storing the image in the Learning folder under D drive with Employee.jpg name.

 

The FromHtmlString method will also accept the following 3 optional parameters to generate the image in a custom format.

 

  • Width: This property is useful to define the width of the image. By default, it is 1024.
  • Format: It is useful to define the image format. By default, it is Jpg.  
  • Quality: It is useful to define the quality of the image from 1 to 100. By default, it is 100

When you execute the above example, you will get the result as shown below.

 

C# Convert HTML to Image Example Result

 

Now, Go to D drive à Open Learning Folder à double click on the Employee.jpg file to open. The generated image will be as shown below.

 

C# HTML to Image Conversion Example Result

 

This is how we can convert the HTML code/string to image using the CoreHtmlToImage library.

Convert URL to Image in C#

By using the CoreHtmlToImage library, we can also convert the URL to image in c#.

 

Following is the example of converting URL to image in c#.

 

using CoreHtmlToImage; using System;
using System.IO;

namespace Tutlane
{
   class Program
   {
      static void Main(string[] args)
      {
        var converter = new HtmlConverter();
        var bytes = converter.FromUrl("https://www.tutlane.com/tutorials");
        File.WriteAllBytes(@"C:\Learning\tutlane.jpg", bytes);
        Console.WriteLine("HTML to Image Conversion Completed");
        Console.WriteLine("Press Enter Key to Exit..");
        Console.ReadLine();
      }
   }
}

In this example, we are using the FromUrl method to generate image for the specified URL. As discussed, like FromHtmlString method, the FromUrl method will also accept 3 optional parameters (width, format, quality) to generate the custom image.

 

When we execute the above example, it will generate the image as shown below.

 

C# Convert Url to Image Example Result

 

This is how we can use the CoreHtmlToImage NuGet package in c# to convert the HTML string to image and generate the image for URL based on our requirements.