Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Screenshot after WebBrowser Navigates

Screenshot after WebBrowser Navigates

Scheduled Pinned Locked Moved C#
comhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VengefulSakhmet
    wrote on last edited by
    #1

    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);

    0 1 Reply Last reply
    0
    • V VengefulSakhmet

      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);

      0 Offline
      0 Offline
      0x3c0
      wrote on last edited by
      #2

      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 the Equals method

      Between the idea And the reality Between the motion And the act Falls the Shadow

      V 1 Reply Last reply
      0
      • 0 0x3c0

        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 the Equals method

        Between the idea And the reality Between the motion And the act Falls the Shadow

        V Offline
        V Offline
        VengefulSakhmet
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups