Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html to jpg with c#

Tags:

html

c#

.net

jpeg

I did some searching to try and generate jpg files from an html page and found one solution called IECapt or something similar that requires IE on the server to work...not what I want.

Here's what I'm looking to do: Generate a jpg image from an html page (the html page will just be text) and then put a watermark over the jpg.

Essentially, I'm creating a "sample" that my users can see which will just be an image created from html (again just straight text). That sample should have a watermark on it as mentioned above. Are there any libraries available to do this with c#? What I'd like is to pass in the url of my page that I want converted to a method and maybe the save path of the jpg, then have it work its magic, and convert that url to a jpg image, toss a watermark on it, then say hooray!

Edit 1

adding some code from the answer below..can't get my head around this:

InitialContainer c = new InitialContainer("<html><body><div align=\"center\">This is my html, does it work here?</div></body></html>");
Bitmap m_Bitmap = new Bitmap(400, 700);
c.Paint(Graphics.FromImage(m_Bitmap));
m_Bitmap.Save(@"C:\test\Test.bmp");


Edit 2

this DOES work.

Bitmap m_Bitmap = new Bitmap(400, 600);
PointF point = new PointF(0,0);
HtmlRenderer.Render(Graphics.FromImage(m_Bitmap), "<html><body><div align=\"center\">This is my html, does it work here?</div></body></html>",point, 500);
m_Bitmap.Save(@"C:\test\Test.bmp");
like image 285
Christopher Johnson Avatar asked Sep 11 '11 17:09

Christopher Johnson


People also ask

How do I convert HTML to JPG?

Open your HTML file in your browser or any viewable tool. Take a snapshot of an area with your screen capture tool (Snipping tool on Windows, for example). Click File > Save as. Select the location and select the Save as type as JPG.

Can JPG be used in HTML?

Most normal image formats (JPEG, GIF, PNG, BMP, TIFF, SVG) will work in most situations most of the time.

Can you convert Mhtml to JPG?

💻 Can I convert MHTML to JPG on Linux, Mac OS or Android? Yes, you can use free Converter app on any operating system that has a web browser. Our MHTML to JPG converter works online and does not require any software installation.


1 Answers

You can use this HtmlRenderer class.

like image 137
SLaks Avatar answered Sep 19 '22 05:09

SLaks