12

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");
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Christopher Johnson
  • 2,629
  • 7
  • 39
  • 70
  • See HTML Renderer [Generate image from HTML markup](https://htmlrenderer.codeplex.com/wikipage?title=Image%20generation) for complete documentation. – Arthur Mar 10 '14 at 14:14

2 Answers2

11

You can use this HtmlRenderer class.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Your answer just saved me a load of stress. Been using a horrible solution that has a dependency on IE. Thanks :) – scartag Sep 11 '11 at 18:13
  • I'm looking at this solution, but I don't see any options to save the rendered HTML as an image. I've downloaded the sample exe and am playing with it now, but so far, I can't find anything about saving the generated html to an image....any pointers? – Christopher Johnson Sep 11 '11 at 18:20
  • 2
    @Christopher: Create a `Bitmap` object, then use `HtmlRenderer` to render to `Graphics.FromImage(...)`. – SLaks Sep 11 '11 at 18:30
  • ok I updated my OP with some code that I'm trying to use to create the image (I can worry about converting to jpg later). For now, it's just creating a file that looks like a blob of text all piled together. I'm sorry for the newbishness, but I've never worked with graphics before... What am I doing wrong here? – Christopher Johnson Sep 11 '11 at 18:46
  • 1
    This link is old, I advice to use: [HtmlRenderer on CodePlex](http://htmlrenderer.codeplex.com/) – Arthur Mar 11 '13 at 13:03
  • link only answer – Hakan Fıstık Jan 17 '18 at 14:50
0

I haven't tried this, but you can try using Control.DrawToBitmap(). To draw the watermark you can go like this:

Image img; //the html image.
Image watermark; //the watermark image.
Point location; //where to draw the watermark on the html image.

Graphics g = Graphics.FromImage(img);
g.DrawImage(watermark, location);
Itamar Marom
  • 525
  • 8
  • 21
  • Perhaps it's possible to do this with WebKit or Gecko. – Itamar Marom Sep 11 '11 at 17:43
  • how does that help me convert raw html to an image though? – Christopher Johnson Sep 11 '11 at 17:44
  • You can use [GeckoFX](http://code.google.com/p/geckofx/) to load the html and then `Control.DrawToBitmap()` to draw it. – Itamar Marom Sep 11 '11 at 17:48
  • so I found this: http://stackoverflow.com/questions/2715385/convert-webpage-to-image-from-asp-net I have it working but I"m a little concerned about something. I had to include System.Windows.Forms in my project (which is an MVC web project) to make this work. Is that bad practice to do on a web project? – Christopher Johnson Sep 11 '11 at 18:05
  • 1
    Why is it bad practice? What other option is there? To just say this is bad practice without a legitimate reason is bad practice. – eaglei22 Mar 24 '16 at 21:05
  • When responding to a web service request one usually tries to avoid expensive calls when serving the response. Making your web server generate an image is an expensive call. Perhaps you are looking for a dedicated image processing service that accepts as input an HTML stream/string and generates an image in the size and format requested. – No Refunds No Returns Jan 17 '18 at 15:31