我正在尝试渲染一个 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.
有什么想法我该怎么做?
谢谢