WebBrowser控件不"呈现" HTML [英] webbrowser control does not "render" html
问题描述
我使用WebBrowser控件正确显示HTML有问题。我的目标是自定义HTML添加到WebBrowser控件,让它显示出来,并保存,作为PNG截图。目前我使用Document.OpenNew和文件撰写(的htmlText)和Application.DoEvents()。但是因为我在后台线程运行此,有时死锁。我知道罪魁祸首是Application.DoEvents(),这是给我的烦恼。
不过,如果我删除,并直接设置HTML来DocumentText财产,我怎么知道什么时候是完全渲染或加载。我用了DocumentCompleted事件,但似乎并没有工作,因为即使在事件触发后保存图像仍然是空的。
我也有螺纹为STA。
下面是现有code:
发个=新主题(新的ThreadStart(的createImage));
th.SetApartmentState(ApartmentState.STA);
th.Start();
th.Join(超时);私人无效的createImage(){
变种浏览器=新的web浏览器();
VAR DOC = browser.Document;
doc.OpenNew(假);
doc.Write(< HTML和GT;<身体GT; ....< /身体GT;< HTML和GT;));
//循环几秒钟
的for(int i = 0; I< 20;我++)
{
Application.DoEvents();
Thread.sleep代码(100);
}
//保存到文件为PNG。
}
下面是code我想:
私人无效的createImage(){
变种浏览器=新的web浏览器();
布尔docComplete = FALSE;
browser.DocumentCompleted + =新WebBrowserDocumentCompletedEventHandler(
(对象发件人,WebBrowserDocumentCompletedEventArgs参数)=>
{docComplete = TRUE; }
);
browser.DocumentText =< HTML和GT; .....;
而(!_docComplete)
{
Thread.sleep代码(100);
}
// 保存图片
// :-(不工作
}
您必须创建一个线程中STA,这仅仅是在后台线程运行web浏览器的方式。
bw.SetApartmentState(ApartmentState.STA);
有没有关于的HTMLDocument等待信息.WRITE方法。如果没有需要,那么你可以添加DocumentCompleted处理不是的DoEvents
私人无效captureImageThread(字符串HTML){
VAR线程=新主题(()=> {
变种浏览器=新的web浏览器();
browser.DocumentCompleted + = browser_DocumentCompleted;
browser.DocumentText = HTML;
Application.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}无效browser_DocumentCompleted(对象发件人,WebBrowserDocumentCompletedEventArgs E){
VAR BR =发件人作为web浏览器;
//保存形象在这里
Application.ExitThread(); //停止线程
}
I am having problem using webbrowser control to correctly display html. My goal is to add custom html to a webbrowser control, have it displayed, and save the screenshot of that as png. Currently I am using Document.OpenNew and Document.Write(htmlText) and Application.DoEvents(). However since I am running this in a background thread, sometimes it deadlocks. I know the culprit is Application.DoEvents() which is giving me troubles.
However, if I remove that and set the html directly to DocumentText property, how do I know when it is fully "rendered" or loaded. I used the DocumentCompleted Event but that does not seem to work since the image that is saved is still empty even after the event fires.
I also have the thread as STA.
Here is the existing code:
Thread th = new Thread(new ThreadStart(createImage));
th.SetApartmentState(ApartmentState.STA);
th.Start();
th.Join(TIMEOUT);
private void createImage() {
var browser = new WebBrowser();
var doc = browser.Document;
doc.OpenNew(false);
doc.Write("<html><body>....</body><html>)");
//loop for few seconds
for(int i=0; i<20; i++)
{
Application.DoEvents();
Thread.Sleep(100);
}
//save to file as png.
}
Here is the code I am trying:
private void createImage() {
var browser = new WebBrowser();
bool docComplete = false;
browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(
(Object sender, WebBrowserDocumentCompletedEventArgs args) =>
{ docComplete = true; }
);
browser.DocumentText = "<html>.....";
while (!_docComplete)
{
Thread.Sleep(100);
}
// save image
// :-( not working
}
You have to create thread as STA, that is only the way to run WebBrowser in a background thread.
bw.SetApartmentState(ApartmentState.STA);
There is no information about waiting in HtmlDocument.Write Method. If is yet required then you can add DocumentCompleted handler instead of DoEvents
private void captureImageThread(string html) {
var thread = new Thread(() => {
var browser = new WebBrowser();
browser.DocumentCompleted += browser_DocumentCompleted;
browser.DocumentText = html;
Application.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
var br = sender as WebBrowser;
// save image here
Application.ExitThread(); // Stops the thread
}
这篇关于WebBrowser控件不&QUOT;呈现&QUOT; HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!