ChevCast
ChevCast

Reputation: 59163

How to render a string containing HTML into an image in C#?

I am developing a web application that has an interactive feedback tool for users. In this application users can click a send feedback button. The button puts up an overlay over their current web page and allows them to drag highlight area DIVs to emphasize certain areas. Once they submit their feedback the HTML of the entire page is passed via AJAX back to the server.

Once on the server I now have a string containing the HTML of the page. From here I would like to run this string through some sort of engine that renders the HTML and builds an image. A sort of round about way of taking a screenshot if you will.

How might one accomplish something like this? Are there engines available that are written in C# and can build up the HTML and render an image?

Upvotes: 4

Views: 2435

Answers (3)

Maxim
Maxim

Reputation: 7348

Check out this framework - http://awesomium.com/

This is exactly what you need.

Set the base URL, this will be needed to resolve any relative URLs

WebCore.SetBaseDirectory("C:\\MyApplication\\MyBaseDirectory");

Then load the HTML -

myWebView.LoadHTML("<p>Hello World!</p>");

Then use the .Render() method, and you'll be able to save the rendered content to an image.

Upvotes: 2

Tocco
Tocco

Reputation: 1705

You can consider usin LLMozLib if you want to go by Gecko.
See more details here

EDIT

There's an ActiveX control to embed Gecko on Windows.
Sample here

EDIT

I got it working on a Windows Forms application.
Using these resources.
It is a csharp wrapper to Gecko ...

That's my sample code ...

public partial class Form1 : Form
{
    public Form1()
    {
        Xpcom.Initialize(@"C:\Users\esouza\Downloads\xulrunner"); //Tell where are XUL bin
        InitializeComponent();
        //geckoWebBrowser1 is an instance of GeckoWebBrowser control that I've dragged on the Form1
        geckoWebBrowser1.DocumentCompleted += new EventHandler(geckoWebBrowser1_DocumentCompleted);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        geckoWebBrowser1.Navigate("http://www.google.com");
    }

    void geckoWebBrowser1_DocumentCompleted(object sender, EventArgs e)
    {
        Bitmap b = new Bitmap(geckoWebBrowser1.Width, geckoWebBrowser1.Height);
        geckoWebBrowser1.DrawToBitmap(b, new Rectangle { X = 0, Y = 0, Width = 800, Height = 600 });
        b.Save("file.bmp");
    }
}

Upvotes: 2

Steve Morgan
Steve Morgan

Reputation: 13091

Very interesting question and I've not got a silver bullet for you.

You're surely going to need a browser engine of some description and then capture the rendered output as a bitmap.

I see from this question that there was a COM wrapper developed for WebKit. Maybe that's a good starting point.

Upvotes: 0

Related Questions