Screenshot after WebBrowser Navigates
-
I'm trying to make my program take a screenshot of a webbrowser after it finishes navigating and all I seem to be able to do is take a screenshot of it before it finishes navigating (a white rectangle). It's probably something very simple that I just keep missing, but am getting too annoyed to see it. I stepped through it and apparently it takes the screenshot when the webBrowserForScreenShot is called after Navigate has been called, but no image is present. However the url is set for the webbrowser to the address it was sent by navigate. I also tried a while loop until webBrowser.ReadyState = complete, but ended up in an infinite loop. I tried adding Application.DoEvents(); as the action in the loop and then end up with a black rectangle. Btw, it does load the page after all is said and done, just too late to be a part of the screenshot. Any help would be great! public ScreenShotWin() { InitializeComponent(); openWebBrowser(); } public void openWebBrowser() { webBrowser1.Navigate("http://google.com"); } private void webBrowserForScreenShot(object sender, WebBrowserDocumentCompletedEventArgs e) { IntPtr htmlWindow = webBrowser1.Handle; CaptureWindowToFile(htmlWindow, "C:\\Interbank.bmp", ImageFormat.Bmp); } this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowserForScreenShot);
-
I'm trying to make my program take a screenshot of a webbrowser after it finishes navigating and all I seem to be able to do is take a screenshot of it before it finishes navigating (a white rectangle). It's probably something very simple that I just keep missing, but am getting too annoyed to see it. I stepped through it and apparently it takes the screenshot when the webBrowserForScreenShot is called after Navigate has been called, but no image is present. However the url is set for the webbrowser to the address it was sent by navigate. I also tried a while loop until webBrowser.ReadyState = complete, but ended up in an infinite loop. I tried adding Application.DoEvents(); as the action in the loop and then end up with a black rectangle. Btw, it does load the page after all is said and done, just too late to be a part of the screenshot. Any help would be great! public ScreenShotWin() { InitializeComponent(); openWebBrowser(); } public void openWebBrowser() { webBrowser1.Navigate("http://google.com"); } private void webBrowserForScreenShot(object sender, WebBrowserDocumentCompletedEventArgs e) { IntPtr htmlWindow = webBrowser1.Handle; CaptureWindowToFile(htmlWindow, "C:\\Interbank.bmp", ImageFormat.Bmp); } this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowserForScreenShot);
This is because a DocumentComplete event fires once for every frame in the WebBrowser. However, the second event argument has a Url property. If
e.Url == webBrowser1.Url
, then the control has finished loading the top-level frame; this is usually the browser window. I got some of this from MSDN forums, here. If the==
operator doesn't work, you could also try theEquals
methodBetween the idea And the reality Between the motion And the act Falls the Shadow
-
This is because a DocumentComplete event fires once for every frame in the WebBrowser. However, the second event argument has a Url property. If
e.Url == webBrowser1.Url
, then the control has finished loading the top-level frame; this is usually the browser window. I got some of this from MSDN forums, here. If the==
operator doesn't work, you could also try theEquals
methodBetween the idea And the reality Between the motion And the act Falls the Shadow
Actually, the URLs matching is another one of fun parts. When it finishes navigating, the URLs match, the page is still not loaded. It turned out to be a threading issue. I created a timer after my webbrowser navigated and checked per every 1000 ticks if the readystate was equal to complete. Very annoying, but it works now. A lesson well learned about trusting documentation. Thank you for your help.