Results 1 to 5 of 5

Thread: [RESOLVED] how to take automatic screenshot of a list of webpages and save it in folder

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2016
    Posts
    6

    Resolved [RESOLVED] how to take automatic screenshot of a list of webpages and save it in folder

    I've these controls on actual form:

    Code:
    Textbox
    (Where at each cycle, there will be a new link with the webbrowser show it);

    Code:
    Webbrowser
    (To check if the page (for example the first on the textbox) is full loaded, and after take an automatic screenshot of the full page).

    Now this program should read each link pasted on the textbox, and a cycle, that will increment, only if, when the page is full loaded, and after screenshots are saved in a default folder, after this can go ahead with the second,ecc. This is the code, i was starting:

    Code:
    Imports System.IO
    Imports System.Drawing
    Imports System.Threading
    Imports System.Windows.Forms
    Public Class Form1
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
                    Dim tempArray As String()
                    tempArray = TextBox1.Lines
                    For counter = 0 To tempArray.Length - 1
                    WebBrowser1.Navigate(tempArray(counter))
                    Next
            End Sub
        End Class
    My problem now is how can i say to the program to wait for page full loading and then take a screenshot and save it to a default folder. (A full page screenshot ) ?

    I tried this for wait until page loading is completed:

    While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
    Application.DoEvents()
    End While
    Next

    but now i have a strange fact, with one link inserted in textbox, it load ok, and when i exit to the program no errors appears. But with 2 link on textbox, the cycle work, fast but it's working, except when i exit to the program it give me an error.

    I tried this part of code for get the screenshots of the web page\s , but the result it's a blank image of that dimension.

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim tempArray As String()
            Dim picture As New Bitmap(1440, 4000)
            tempArray = TextBox1.Lines
            For counter = 0 To tempArray.Length - 1
                WebBrowser1.Width = 1440
                WebBrowser1.Height = 4000
                WebBrowser1.Navigate(tempArray(counter))
                WebBrowser1.DrawToBitmap(picture, New Rectangle(WebBrowser1.Location.X, WebBrowser1.Location.Y, WebBrowser1.Width, WebBrowser1.Height))
                picture.Save("C:\test.jpeg")
                While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
                    Application.DoEvents()
                End While
            Next
        End Sub
    After a while i was thinking i made it with this (but the form sometime need to be full screen or not working, but this isn't important because it's working too with normal form size, except sometimes it give a blank image, i think is webbrowser.drawtobitmap() with the refresh):

    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim tempArray As String()
            Dim picture As New Bitmap(1440, 4000)
            tempArray = TextBox1.Lines
            For counter = 0 To tempArray.Length - 1
                WebBrowser1.Width = 1440
                WebBrowser1.Height = 4000
                WebBrowser1.Navigate(tempArray(counter))
                WebBrowser1.DrawToBitmap(picture, New Rectangle(WebBrowser1.Location.X, WebBrowser1.Location.Y, WebBrowser1.Width, WebBrowser1.Height))
                Me.WebBrowser1.Refresh()
                picture.Save("C:\test1.Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
                While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
                    Application.DoEvents()
                End While
            Next
        End Sub
    And this is the result (not maded with cycled but one by one for test, another strange this is the webbrowser can't read well how much links textbox have):

    http://i.stack.imgur.com/Z9qrl.jpg (with the google page)

    http://i.stack.imgur.com/fmaqv.jpg (with the youtube page)

    http://i.stack.imgur.com/iIqqY.jpg(with the tvdb page)

    And with this, i want to know if it's possible to fix the issues of blank images and long name error with the save function and webbrowser1.documentTitle into picture.save, like you can see below.

    For the name picture file with every cycle(sometimes working and other not) with this:

    picture.Save("C:\" + WebBrowser1.DocumentTitle + ".Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)

    but have a issues too there, because with long name it give me the System.NotSupportedException, and when get webpage like google it get two files, one without name and blank and another with name and content (but usually blank).

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: how to take automatic screenshot of a list of webpages and save it in folder

    That's a pretty bad solution because of the Busy Wait (spinning around an Application.DoEvents), which means that the CPU will be totally pegged while doing nothing at all. I have a thread over in the General Developer section where I show the increased power consumption by the computer while running code like that. It's one of the few things you can do in programming that could cause physical damage to your computer, though only if your cooling system is compromised. Still, it's bad enough anyways.

    The solution is much more complicated, though. The reason for those busy waits is just that people want their program to be linnear such that it goes through the method line by line. If you really want to do it that way, add a Sleep statement into the busy loop. Sleep(100) will be insignficant to you, as it would mean that the process will sleep for 0.1 seconds at a time, but the cost to the CPU will drop WAY down.

    Better still would be more complicated. You are already moving the sites to an array. The next step would be to call a method that would navigate to the url...and that's it. You'd then handle the DocumentCompleted event of the WebBrowser. At one point, that event was raised when the document was fully loaded, but that was back when websites were simple. These days, you might get that event several times for each site as different pieces load. So, when it happens, you'd then check whether the loading really is complete (perhaps by checking that ReadyState, if that is sufficient). If it is, then you'd get the next URL from your array and call that navigate method again.

    In other words, you'd get the array and navigate to the first page. You'd then be monitoring DocumentCompleted events until the page is fully loaded, at which point you'd navigate to the next page (from the DocumentCompleted event handler) and so on until you have navigated to each URL in the array. There would be a loop, but not a literal loop. It would just be that the completion of the current page would trigger the loading of the next page, and so on.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2016
    Posts
    6

    Re: how to take automatic screenshot of a list of webpages and save it in folder

    Wow thanks for the information, (i'm new to visual basic from a few days) and i just confirm what you said because i see it with task manager, cpu % skipped to 100 in a second, thank you again for the information. And i want to ask a thing why i get sometime a blank image of the websites in webbrowser through the picture.save fuction, and why with long name site title it give me an error ? i tried with this one:
    Code:
    picture.Save("C:\" + tempArray(counter) + ".Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
    and with this:
    Code:
    picture.Save("C:\" + TextBox1.Lines(counter) + ".Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
    but with both it give me this error:
    System.ArgumentException
    The only one answer i can think is because of special characther of url, this can easy prove with the rename of windows, i want like to do for example... name+index, so i need to create another cycle with index that increment each time it go next with links
    or i think i can just use this cycle i have for this thing of the name.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,943

    Re: how to take automatic screenshot of a list of webpages and save it in folder

    Is the WHOLE error message just "System.ArgumentException"? When it happens, highlight all of "C:\" + tempArray(counter) + ".Jpeg" and press Shift+F9. Does it look right?
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2016
    Posts
    6

    Re: how to take automatic screenshot of a list of webpages and save it in folder

    Yes it's right it give this :
    Name:  Immagine.jpg
Views: 639
Size:  41.3 KB
    (I was testing with some kind of code)
    It's italian language, but i can translate for you, so:
    Espressione=Expression
    Nome=name
    Valore=value
    tipo=type
    This error give it to me specially with this kind of link http://thetvdb.com/?tab=seasonall&id=73616&lid=15 but ever if i say to add the webbrowser1.documettitle to the path give me same error.
    Edit
    I think i found a walk-around for long name problem with this:
    Code:
    picture1.Save("C:\test" + counter2.ToString + ".Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
    But i have a strange fact, when i first push the button for do the process, it give a blank page on the destination path, but if i do it a second time it's working.
    This is now my actual code:
    Code:
    Imports System.IO
    Imports System.Drawing.Graphics
    Imports System.Threading
    Imports System.Windows.Forms
    Public Class Form1
        Dim tempArray1 As String()
        Dim picture1 As New Bitmap(1440, 4000)
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For counter2 As Integer = 0 To TextBox1.Lines.Length - 1
                For counter As Integer = 0 To TextBox1.Lines.Length - 1
                    tempArray1 = TextBox1.Lines
                    WebBrowser1.Width = 1440
                    WebBrowser1.Height = 4000
                    WebBrowser1.Navigate(tempArray1(counter))
                    Thread.Sleep(500)
                    WebBrowser1.DrawToBitmap(picture1, New Rectangle(WebBrowser1.Location.X, WebBrowser1.Location.Y, WebBrowser1.Width, WebBrowser1.Height))
                Next
                picture1.Save("C:\test" + counter2.ToString + ".Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg)
            Next
        End Sub
    End Class
    And there are the results:
    Name:  Immagine.png
Views: 504
Size:  3.4 KB
    Name:  Immagine2.png
Views: 476
Size:  6.2 KB
    EDIT 2:
    I just found a program already exist to do this, if someone's interesting just search for webpage thumbnailer (trial version have a cross on downloaded thumnail) on google or install the extension for firefox grab em all. But... i still want to know how to do that..
    Last edited by axel48; Mar 6th, 2016 at 04:15 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width