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.
After that, Select .NET Framework version and click on Create button to create the project as shown below.
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.
After installing the CoreHtmlToImage package, open Program.cs file and write the code as shown below.
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.
When you execute the above example, you will get the result as shown below.
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.
This is how we can convert the HTML code/string to image using the CoreHtmlToImage library.
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#.
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.
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.