WebBrowser w1; private void button1_Click(object sender, EventArgs e) { Thread t = new Thread(p => { w1 = new WebBrowser(); w1.ObjectForScripting = this; w1.Navigate(System.Environment.CurrentDirectory + @"\test.html"); w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetBitmap); }); t.TrySetApartmentState(ApartmentState.STA); t.Start(); } private void GetBitmap(object sender,WebBrowserDocumentCompletedEventArgs e) { HtmlElement element = w1.Document.Body; int w = element.ScrollRectangle.Width + element.OffsetRectangle.Width; int h = element.ScrollRectangle.Height + element.OffsetRectangle.Height; w1.Width = w; w1.Height = h; using (Bitmap bmp = new Bitmap(w, h)) { w1.DrawToBitmap(bmp, element.ClientRectangle); string fileName = "d:\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bmp"; bmp.Save(fileName); } }
问题:运行时没进GetBitmap方法,刚开始接触线程,求详细指点.. PS:不用线程,是能进GetBitmap方法的 异步事件完成,应该是异步还没有执行完,线程就结束了, 所以你可以在 w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetBitmap); 这句后面,设置一个信号量,让线程不要结束,然后在 private void GetBitmap(object sender,WebBrowserDocumentCompletedEventArgs e) 方法里,重置这个信号量。 WebBrowser w1; AutoResetEvent autoResetEvent = new AutoResetEvent(false); private void button1_Click(object sender, EventArgs e) { Thread t = new Thread(p => { w1 = new WebBrowser(); w1.ObjectForScripting = this; w1.Navigate(System.Environment.CurrentDirectory + @"\test.html"); w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetBitmap); autoResetEvent .WaitOne(); }); t.TrySetApartmentState(ApartmentState.STA); t.Start(); } private void GetBitmap(object sender,WebBrowserDocumentCompletedEventArgs e) { HtmlElement element = w1.Document.Body; int w = element.ScrollRectangle.Width + element.OffsetRectangle.Width; int h = element.ScrollRectangle.Height + element.OffsetRectangle.Height; w1.Width = w; w1.Height = h; using (Bitmap bmp = new Bitmap(w, h)) { w1.DrawToBitmap(bmp, element.ClientRectangle); string fileName = "d:\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bmp"; bmp.Save(fileName); } //即通知正在等待的线程有事件发生 autoResetEvent.Set(); } WebBrowser w1; AutoResetEvent autoResetEvent = new AutoResetEvent(false); private void button1_Click(object sender, EventArgs e) { Thread t = new Thread(p => { w1 = new WebBrowser(); w1.ObjectForScripting = this; w1.Navigate(System.Environment.CurrentDirectory + @"\test.html"); w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetBitmap); while (w1 .ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } }); t.TrySetApartmentState(ApartmentState.STA); t.Start(); t.Join(); } private void GetBitmap(object sender,WebBrowserDocumentCompletedEventArgs e) { HtmlElement element = w1.Document.Body; int w = element.ScrollRectangle.Width + element.OffsetRectangle.Width; int h = element.ScrollRectangle.Height + element.OffsetRectangle.Height; w1.Width = w; w1.Height = h; using (Bitmap bmp = new Bitmap(w, h)) { w1.DrawToBitmap(bmp, element.ClientRectangle); string fileName = "d:\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bmp"; bmp.Save(fileName); } //即通知正在等待的线程有事件发生 autoResetEvent.Set(); }
|