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");
1

2 Answers 2

11

You can use this HtmlRenderer class.

6
  • Your answer just saved me a load of stress. Been using a horrible solution that has a dependency on IE. Thanks :)
    – scartag
    Sep 11, 2011 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? Sep 11, 2011 at 18:20
  • 2
    @Christopher: Create a Bitmap object, then use HtmlRenderer to render to Graphics.FromImage(...).
    – SLaks
    Sep 11, 2011 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? Sep 11, 2011 at 18:46
  • 1
    This link is old, I advice to use: HtmlRenderer on CodePlex
    – Arthur
    Mar 11, 2013 at 13:03
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);
6
  • Perhaps it's possible to do this with WebKit or Gecko. Sep 11, 2011 at 17:43
  • how does that help me convert raw html to an image though? Sep 11, 2011 at 17:44
  • You can use GeckoFX to load the html and then Control.DrawToBitmap() to draw it. Sep 11, 2011 at 17:48
  • so I found this: stackoverflow.com/questions/2715385/… 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? Sep 11, 2011 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, 2016 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.