.net html to image with managed code
I have service which converts HTML to image and开发者_高级运维 sends back.
At present I am using WebBrowser control to do that. But it is far from perfect. During peak hours service crashes or gives Null reference exception. Also it is not lightweight on memory. I looking for lightweight managed option to this job.
Kindly let me know for any open source project or for any other managed control which will do this job.
Update: I have very simple HTML to render. It is only with table, paragraph and stylesheet for font and background-color. There is not Javascript, element float or other complex layout.
You can try Awesomium
using System;
using AwesomiumSharp;
using System.Threading;
using System.Diagnostics;
namespace AwesomiumSharpBasic
{
class Program
{
static void Main( string[] args )
{
// Display some informative message. Loading the page
// may take a while depending on your internet
// connection speed.
Console.WriteLine( "Getting a 1024x768 snapshot" +
"of http://www.awesomium.com ..." );
// Create a WebView.
// WebView implements IDisposable. You can dispose and
// destroy the view by calling WebView.Close().
// Here we demonstrate wrapping it in a using statement.
using ( WebView webView =
WebCore.CreateWebView( 1024, 768 ) )
{
// Variable used to announce
// that the page has loaded.
bool finishedLoading = false;
// Load a page in the view.
webView.LoadURL( "http://www.awesomium.com" );
// Handle the LoadCompleted event to monitor
// page loading.
webView.LoadCompleted += ( sender, e ) =>
{
finishedLoading = true;
};
// Wait for the page to load.
while ( !finishedLoading )
{
Thread.Sleep( 100 );
// WebCore provides an Auto-Update feature
// for UI applications. A console application
// has no UI and no synchronization context
// so we need to manually call Update here.
WebCore.Update();
}
// Render to a pixel buffer and save the buffer
// to a .png image.
webView.Render().SaveToPNG( "result.png", true );
}
// Start the application associated with .png files
// and display the file.
Process.Start( "result.png" );
// Shut down Awesomium before exiting.
WebCore.Shutdown();
}
}
}
Try WebKit .Net.
I finally settled on WebBrowser control due to not so valid HTML available at my end.
I should mention following post which should solve problem for other guys who got valid HTML. It mentions few open source and paid controls.
http://www.codinghorror.com/blog/2004/10/managed-html-rendering.html
精彩评论