Convert HTML To Image in .NET Core 2.0

N P picture N P · May 2, 2018 · Viewed 10.5k times · Source

I want to convert HTML code to Image(png/jpg) on Web Server and then send image link in email in my .NET Core 2.0 application. I don't want to purchase any third party library like NReco or EVo.

Is there any other way to convert HTML To Image in dotnet core 2.0?

Answer

Andrei picture Andrei · Dec 2, 2018

I use net-core-html-to-image library that embeds wkhtmltoimage tool. The library is very simple to use. There is a nuget package:

Install-Package CoreHtmlToImage

If you want to convert HTML string to image:

var converter = new HtmlConverter();
var html = "<div><strong>Hello</strong> World!</div>";
var bytes = converter.FromHtmlString(html);
File.WriteAllBytes("image.jpg", bytes);

Or for URLs:

var converter = new HtmlConverter();
var bytes = converter.FromUrl("http://google.com");
File.WriteAllBytes("image.jpg", bytes);