我正在尝试渲染HTML代码以将其导出为图像,然后使用该图像将其放入PDF文档中。我在asp.net/C# Web应用程序中完成所有工作。我遇到的问题是,当我进行多次操作时,它会给我这个错误:
这是我的代码:
有什么想法我该怎样做这个?谢谢。
You are attempting to re-initialize the WebCore. The WebCore must only be initialized once per process and must be shut down only when the process exits.
这是我的代码:
WebCore.Initialize(new WebConfig(), false);
using (WebView webView = WebCore.CreateWebView((pdfWidth - 80).ToString().ToInt() + 20, (pdfHeight / 2).ToString().ToInt() + 20))
{
webView.LoadHTML(html);
while (webView.IsLoading || !webView.IsDocumentReady)
WebCore.Update();
Thread.Sleep(1100);
WebCore.Update();
string fileName = Server.MapPath("result." + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString() + ".png");
BitmapSurface surface = (BitmapSurface)webView.Surface;
surface.SaveToPNG(fileName, true);
WebCore.Shutdown();
}
我发现不能每次网页渲染时都初始化WebCore,所以我将其放在Global.asax中。这样做后,在调试时,又出现了另一个错误:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt. The current thread is not currently running code or the call stack could not be obtained.
有什么想法我该怎样做这个?谢谢。
WebCore.Initialize
应该放在Application_Start
中,而WebCore.Shutdown
应该放在Global.asax
的Application_End
中。 - Dustin Kingen