I have string
with the page's HTML, is there any way to save it as an image? Render the entire page as an image, and if possible, store it in MemoryStream
.
I have string
with the page's HTML, is there any way to save it as an image? Render the entire page as an image, and if possible, store it in MemoryStream
.
It's a C # library. I recommend that you do not need anything beyond the DLL itself, without any other dependencies, if you want to save it in image.
Example (removed from here ):
namespace HtmlToBmpImageDemo
{
class Program
{
static void Main(string[] args)
{
Bitmap m_Bitmap = new Bitmap(400, 600);
PointF point = new PointF(0, 0);
SizeF maxSize = new System.Drawing.SizeF(500, 500);
HtmlRenderer.HtmlRender.Render(Graphics.FromImage(m_Bitmap),
"<html><body><p>This is a shitty html code</p>"
+ "<p>This is another html line</p></body>",
point, maxSize);
m_Bitmap.Save(@"C:\Test.bmp }
}
}
I also recommend this library as they have direct save in * .pdf support. It gives a search in Nuget for HtmlRenderer.PdfSharp
.
Example (removed from here )
public static Byte[] PdfSharpConvert(String html)
{
Byte[] res = null;
using (MemoryStream ms = new MemoryStream())
{
var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
pdf.Save(ms);
res = ms.ToArray();
}
return res;
}
wkhtmltopdf encapsulates a console application for .NET. According to their page in GitHub , it's simple to convert HTML to PDF using the tool.
I will not go into the subject because there is already an answer speaking about it.
It is protected by LGPL , so I recommend you take a read, depending on your case, it may be a complication. Of course, reading the official "license agreement" is essential, but there are some tools that can help you, such as Choose a License , which I judge quite reliable. Another curious fact about the site is it is opensource .
The easiest way for you to do this is to use an external utility, in this case WKHtmlToImage
.
To do this, you will need to write your HTML
to disk, using System.IO.File.WriteAllText(string path, string contents)
.
The second step is to instantiate a System.Diagnostics.Process
, set the processo.StartInfo.FileName
to the location where WKHtmlToImage
is and pass the due arguments to processo.StartInfo.FileName
If you need to know what arguments are expected by WKHtmlToImage
, you can check the Manual
, although in your case, I believe wkhtmltoimage <input file> <output file>
will suffice.
When the process finishes, you can read the disk image.