Render HTML as an Image

Solution 1:

What you can do is create an aspx page that changes the response type to be in the format you want and then put the image into the stream. I created a barcode generator that does a similar thing. Excluding all the formalities of generating the image, you'll Page_Load will look something like this:

Bitmap FinalBitmap = new Bitmap();
MemoryStream msStream = new MemoryStream();

strInputParameter == Request.Params("MagicParm").ToString()

// Magic code goes here to generate your bitmap image.
FinalBitmap.Save(msStream, ImageFormat.Png);

Response.Clear();
Response.ContentType = "image/png";

msStream.WriteTo(Response.OutputStream);

if ((FinalBitmap != null)) FinalBitmap.Dispose();

and that's it! Then all you have to do in your image is set the URL to be something like RenderImage.aspx?MagicParm=WooHoo or whatever you need. That way you can have it render whatever you want to specify.

Solution 2:

You can render html to a bitmap using the WebBrowser control in either a winforms or console application.

An example of this can be found here: http://www.wincustomize.com/articles.aspx?aid=136426&c=1

The above example can be modified to run in ASP.Net by creating a new STAThread and performing an Application.Run on it to start a new message loop.

Solution 3:

PHP/Ruby Alternative


If you have accessed this question and are actually looking for soething that will work without Windows, you can try the KHTML library: http://wiki.goatpr0n.de/projects/khtmld

The website has a ridiculous name I admit, but I can assure you it is genuine. Other related pages are: the sourceforge page http://khtml2png.sourceforge.net/

Solution 4:

Try PDFSharp...it's not exactly a "take this HTML and make a PDF" but with a small amout of fiddling you can easily make a PDF out of the info you are using to make the HTML.

Solution 5:

MARKUP ONLY ALTERNATE SOLUTION

Use SVG and XSLT to transform the html data into an image that can be rendered/saved/etc.

I'll admit that at first it was tedious getting this to work because of all of the coordinates, but well worth the effort once it is running.