none
Webbrowser Document (VB) RRS feed

  • Question

  • Hello community,

    I have created a Windows Form. In the Form a have loaded a Webbrowser called WbrWebbrowser. In the design mode i have in the property window ,at the URL place, set the site string: http://www1.wetter3.de/animation.html. When i run the form, the website starts directly and shows the desired webpage. Till so far nothing wrong. When you right click the mouse, a menu is shown. If you choose for "Save as", then a SaveFileDiagalog is started. From that moment you see the name of the file, in a special textbox: "00_1.Gif". Mark! This is not a string file. But a Image file!. And a Image is a object, a think. The file shall be saved in your map MyDocuments. When you have done that and you double click on it, in showFiledialog mode, then Photo's start up, and shows you the picture of the site. The same thing you get when you choose for the menu item "Properties", after right click, instead of "Save as". Microsoft shows you all details of the webpage: FileName, lastcreatedDate, LastModifiedDate etc.

    What do i want?

    I want without a SaveFileDialog.show() programmaticaly get that file name, and save it into a row of a Access database that has the type Appendix.

    What have i done till so far:

    I have examined the HtmDocument and the HtmlElements. With the collection statement:

    Dim Elems AsHtmlElementCollection = WbrWebBrowser.Document.GetElementsByTagName("IMG")

    But also with "HTML" TagName. And have in a For Each Elem in Elems construction searched for that element that contains the desired name en URL. I have examined the DomElement. And find that what i searching for. In the IHTMLImg elements. But that are all textfiles. Typed with Strings. That are not objects or Images! So i realised that i was searching in the wrong way.

    My question is: How do i code it? I have searched in msdn for weeks now. And i can't find a good sample that pointed me in the right direction. I have done my best and i almost want to give it up. I hope so that someone can help me. If you can help me with a little code sample, please write it in the language VB.

    Best regards,

    Willem.

    Wednesday, July 25, 2018 12:11 PM

Answers

  • Your question is unclear, but I suspect that is mostly due to language barriers... the issue is being lost in the translation to English.

    But if I am to guess at your problem, I suspect that you are trying to save an image that is present on a webpage.  However, when you search the source HTML you are only finding a string which points to the image resource.  This is the way HTML works.  The web browser makes multiple calls to the web server to get all of the content on a page.  The data of an image is not contained within the HTML of a web page.  The web browser reads that the HTML contains an image tag and then makes another call to the web server to retrieve that image.

    I suspect that what you want to do is a process of multiple steps.  First you must find the HTML tag for the image you want.  Then you must read the URL of that image from the tag.  Finally you must create new web request which downloads that image's data bytes.

    It would appear that you have code to find all HTML image tags.  You must still figure out which tag represents the particular image you want to save.  Once you have the URL of the image from the correct tag you can use one of a number of methods to download that image using the URL you found.  My.Computer.Network.DownloadFile() is one such method that you could use to pass the URL to the image file and a path and filename on the local computer where you want to save the downloaded image.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by HofsteeWillem Friday, July 27, 2018 11:00 AM
    Thursday, July 26, 2018 3:22 AM
    Moderator
  • No you should not need any additional references.

    Once you have the correct <IMG> tag with your current code, you need to take the "src" URL in that tag and use it to make a separate call to download that image.  You can use the DownloadFile method I mentioned and give it the URL from the IMG tag along with the local file path where you want the image to be saved.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by HofsteeWillem Saturday, July 28, 2018 10:07 AM
    Friday, July 27, 2018 2:07 PM
    Moderator
  • Hi Willem,

    You shouldn't have to "open" anything.  The Elem variable is the IMG element.  For example, if I use the website you posted, your code, and just get the src attribute and put it into a textbox so that I can see the address, I get the following:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WbrWebBrowser.ScriptErrorsSuppressed = True
        WbrWebBrowser.Navigate("http://www1.wetter3.de/animation.html")
    End Sub
    Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
        Dim Elems As HtmlElementCollection
        If (WbrWebBrowser.Document IsNot Nothing) Then
            Elems = WbrWebBrowser.Document.GetElementsByTagName("IMG")
            For Each Elem As HtmlElement In Elems
                Dim src = Elem.GetAttribute("src")
                RichTextBox1.AppendText(src)
                RichTextBox1.AppendText(vbCrLf)
            Next
        End If
    End Sub
    

    The contents of RichTextBox1 contain:

    http://www1.wetter3.de/Animation_12_UTC/00_1.gif
    http://www1.wetter3.de/Buttons/button_startseite_dt.png
    http://www1.wetter3.de/einpixel.jpg
    http://www1.wetter3.de/einpixel.jpg
    http://www1.wetter3.de/Buttons/button_zumTut_dt.png
    http://www1.wetter3.de/einpixel.jpg
    http://sponsorads.de/de/images/arrow_black.png
    http://www1.wetter3.de/Buttons/button1a_dt.png
    http://www1.wetter3.de/Buttons/button2_dt.png
    http://www1.wetter3.de/Buttons/button3_dt.png
    http://www1.wetter3.de/Buttons/button4_dt.png
    http://www1.wetter3.de/Buttons/button5_dt.png
    http://www1.wetter3.de/Buttons/button6_dt.png
    http://www1.wetter3.de/Buttons/button7_dt.png
    http://www1.wetter3.de/Buttons/button8_dt.png

    So the very first result appears to be the URL to the image you want to download and save.

    That being the case, you should be able to just use the first IMG tag without looping through all of them.

    One thing I discovered while testing this is that the site will throw a "forbidden" error if the web request does not contain a known user agent header.  Because of this, we can't use the DownloadFile() method.  Instead we must manually create a WebRequest and get the response.

    Here is an example that saves the map image.  Be sure to edit this code to use a valid file path on your computer:

    Dim Elems As HtmlElementCollection
    If (WbrWebBrowser.Document IsNot Nothing) Then
        Elems = WbrWebBrowser.Document.GetElementsByTagName("IMG")
        Dim targetElement = Elems.Item(0)
        Dim sourceURL = targetElement.GetAttribute("src")
        Dim request = System.Net.WebRequest.CreateHttp(sourceURL)
        request.UserAgent = "Mozilla/5.0"
        Using response = request.GetResponse
            Using img = Image.FromStream(response.GetResponseStream)
                img.Save("E:\Data\testmap.gif")
            End Using
        End Using
    End If
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by HofsteeWillem Friday, August 3, 2018 4:18 PM
    Wednesday, August 1, 2018 7:01 PM
    Moderator

All replies

  • I don't understand what you want.


    Sam Hobbs
    SimpleSamples.Info

    Thursday, July 26, 2018 1:39 AM
  • Your question is unclear, but I suspect that is mostly due to language barriers... the issue is being lost in the translation to English.

    But if I am to guess at your problem, I suspect that you are trying to save an image that is present on a webpage.  However, when you search the source HTML you are only finding a string which points to the image resource.  This is the way HTML works.  The web browser makes multiple calls to the web server to get all of the content on a page.  The data of an image is not contained within the HTML of a web page.  The web browser reads that the HTML contains an image tag and then makes another call to the web server to retrieve that image.

    I suspect that what you want to do is a process of multiple steps.  First you must find the HTML tag for the image you want.  Then you must read the URL of that image from the tag.  Finally you must create new web request which downloads that image's data bytes.

    It would appear that you have code to find all HTML image tags.  You must still figure out which tag represents the particular image you want to save.  Once you have the URL of the image from the correct tag you can use one of a number of methods to download that image using the URL you found.  My.Computer.Network.DownloadFile() is one such method that you could use to pass the URL to the image file and a path and filename on the local computer where you want to save the downloaded image.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by HofsteeWillem Friday, July 27, 2018 11:00 AM
    Thursday, July 26, 2018 3:22 AM
    Moderator
  • In the design mode i have in the property window ,at the URL place, set the site string: http://www1.wetter3.de/animation.html. When i run the form, the website starts directly and shows the desired webpage. Till so far nothing wrong. When you right click the mouse, a menu is shown. If you choose for "Save as", then a SaveFileDiagalog is started.

    Not for me and that is not what normaly happens for me. More specifically, if I create a project and only do as much as you describe then there is no menu when we right-click. And by the way, the menu is called a context menu. I don't get a context menu when in a page in a WebBrowser unless I do something more. I know how to do it. So I assume something is being done in your application that causes a context menu to show. Since it is unique to your application, we can't help, at least not until we know what the context menu is doing. I think that everything else in your question is relevant to that code that we know nothing about. You are assuming it is Microsoft code but it probably is not and that is why I am confused.

    I have written an article that is likely to help but the code is C# so if you insist upon VB code then that article can't help.

    Try creating a new project and just put a WebBrowser in the form and put the URL in the Url property. For me, I get script errors. I can't use the URL you say you are using. If I use "http://google.com" then I don't get a context menu when I right-click.



    Sam Hobbs
    SimpleSamples.Info


    Thursday, July 26, 2018 4:20 AM
  • Hi Reed Kimble,

    Thank you for your reply!

    I am sorry for my bad English. Maybe this year or else next year i start up learning English again. Words and grammar. I hope that it will help.

    But what you wrote : "I suspect that you are trying to save an image that is present on a webpage.", what you did suspected is right! (A copy of your sentense). You wrote about a web server. My question is: Do i have to import System.Web.Service? Or maybe System.Device? I mean the references.

    Is that what you try to explain to me?

    Best regards,

    Willem.

    Friday, July 27, 2018 11:32 AM
  • Hi Sam,

    Thank you for your reply. Sorry for confusing you. But it is rather easy. And i think that it works everywhere on earth the same. It works also when you open Edge, for example, or I.E.

    Just open a webpage with a URL you know. Then rigth-click with the mouse. Then a CONTEXT menu appear. I did not know that it was called so. But oké. Context menu. There is a lot to choose. But one of the items is called "Save As". And when you click that, an other window appears. And that window is called: SaveFileDialoge. This window shows one or more textboxes. One textbox is the "FileName" textbox. And that shows the name of the image or page you look at.

    Try it with this one:https://nl.sat24.com/nl

    This site will give you a vision of the Netherlands out of the satelite. If you click-right, you can save the image. I am very sure about that. And when you change it in "https://Us.sat24.com/us " you get America! And in a other way you can in the first URL navigate to America.

    Try it out! Succes!

    Best regards,

    Willem.


    Friday, July 27, 2018 12:04 PM
  • No you should not need any additional references.

    Once you have the correct <IMG> tag with your current code, you need to take the "src" URL in that tag and use it to make a separate call to download that image.  You can use the DownloadFile method I mentioned and give it the URL from the IMG tag along with the local file path where you want the image to be saved.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by HofsteeWillem Saturday, July 28, 2018 10:07 AM
    Friday, July 27, 2018 2:07 PM
    Moderator
  • Just open a webpage with a URL you know. Then rigth-click with the mouse. Then a CONTEXT menu appear. I did not know that it was called so. But oké. Context menu. There is a lot to choose. But one of the items is called "Save As". And when you click that, an other window appears. And that window is called: SaveFileDialoge. This window shows one or more textboxes. One textbox is the "FileName" textbox. And that shows the name of the image or page you look at.

    You did not read what I said. I said that by default there is no context menu in a WebBrowser control. I tried it before I replied and I said that.

    The following is the context menu for Firefox,

    There is no "Save As". The following is the context menu for Edge.

    I can't capture Edge and Chrome. I see a "Save As" for Chrome but not for IE. IE is the important one; the WebBrowser control is IE.

    If you insist I am wrong then I can't help you.



    Sam Hobbs
    SimpleSamples.Info

    Friday, July 27, 2018 2:30 PM

  • You did not read what I said. I said that by default there is no context menu in a WebBrowser control. I tried it before I replied and I said that.



    There should be... by default the "IsWebBrowserContextMenuEnabled" property for the WebBrowser control is set to True.  So as long as there is some content loaded in the browser control, there is a default context menu (the menu doesn't show if nothing has been loaded in the control).

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, July 27, 2018 2:36 PM
    Moderator
  • There should be... by default the "IsWebBrowserContextMenuEnabled" property for the WebBrowser control is set to True.

    Obviously you did not try it. Sure, that property is true, but there is no context menu.



    Sam Hobbs
    SimpleSamples.Info

    Friday, July 27, 2018 3:38 PM
  • There should be... by default the "IsWebBrowserContextMenuEnabled" property for the WebBrowser control is set to True.

    Obviously you did not try it. Sure, that property is true, but there is no context menu.



    Sam Hobbs
    SimpleSamples.Info

    Wow, really?  What an attitude...

    First of all, that isn't the right property:

    And then here is my junk app I've been testing with for a week or two:

    So yes, I tried it.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, July 27, 2018 4:23 PM
    Moderator
  • Wow, really? What an attitude...

    Yes I was wrong. But the personal attack is unnecessary.

    First of all, that isn't the right property:

    It is the right property that I intended to show. I am showing the ContextMenu property that was null. I don't know why it was but it was at the time I ran my program as described by me previously.

    I think there is a problem with IE in my system. I apologize for the mistake but it was a mistake, not attitude.



    Sam Hobbs
    SimpleSamples.Info

    Friday, July 27, 2018 4:45 PM
  • Mistakes are fine; it was the "obviously you didn't try it" comment that I found to exhibit poor attitude.  Please accept my apology as well and we'll move past it.

    As for the two properties, the ContextMenu property allows you to attach your own ContextMenu instance to the control; it is provided by the base Control class (and is generally deprecated in favor of the ContextMenuStrip property).  That property does not have any impact on getting the default context menu for a web browser.  The IsWebBrowserContextMenuEnabled property is provided by the WebBrowser control and this is the one that will provide a default context menu if there is a document loaded into the WebBrowser control.  If no document has been loaded, nothing happens when you right click on the browser control.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, July 27, 2018 6:48 PM
    Moderator
  • I apologize for being too strong but there are some people around here that just won't listen. There are many things in this question that seem wrong to me and it is not a matter of language. Saying (at least implying) that all browsers work the same in this context is incorrect. In the WebBrowser control, there is not a "SaveAs"; there is a "Save Picture As" if we right-click on an image but the question does not say explicitly that the right-click is on an image. I understand that we don't always know the correct terminology so I understand not knowing that the popup menu is called a context menu and I can and will overlook language issues but I think there are things in this question that are not accurate and not for either of those reasons.


    Sam Hobbs
    SimpleSamples.Info

    Friday, July 27, 2018 8:19 PM
  • Thank you Reed,

    i have seen the "scr" and i will try to download it with your method that you have given me. And i shall do it without any additional reference.

    Best Regards,

    Willem.

    Saturday, July 28, 2018 10:11 AM
  • Hi Sam,

    Oké! You are right! I am working with I.E. and seventh line from the top line of the context menu says"Save Page As".  And the last line of the context menu, that is the bottom line, says "Properties". I translate it into Englisch. I see that there are some differences of browsers. The samples you show me in your reply tells me that.

    But anyhow Sam, I have get from Reed a good idea that i am going to work out.

    Thank you for replying,

    Best regards,

    Willem.

    Saturday, July 28, 2018 10:31 AM
  • Hoi Sam and Reed,

    I suppose that my question was a very good one for creating a discussion. But don't fight each other to hard. Just stay discussing.

    Sam is right when he wants it clear that the item of the popup or context menu is called: "Save Picture As". I was not right in writing "Save As". Sorry for that, Sam. It is "Save Picture As".

    It is also right that the Webbrowser control has the property "IsWebBrowserContextMenuEnabled". And that property has the value True at mine computer. Here at home. And it is possible that the context menu is the context menu from the Webbrowser control. And not from I.E. However, i don't see any differences between the contex menu's at all.

    I explain:

    Outside my app i start up I.E. browser. And i give in a URL. The Webside start up. When i click-right with the mouse in the web page, i get the context menu. The same context menu we talked about. When i click on the item "Save Picture As", an other screen or window appears. I call that the "SaveFileDialoge" Window. That window contains textboxes. One of the textboxes is called: "File name:". And that textbox is filled with the name of the current web page. And so i ask you: "How did Microsoft do that? ".

    But now the most confusing point:

    When i do the same thing in my app, thus not from outside the app., i get the same context menu as i got outside my app. With the same "SaveFileDialog" window as outside my app, after right-click with the mouse and click the item "Save Page As". And also the textbox "File Name: ". And the textbox also is filled with the name of the current web Page.

    So! There are no differences. Nor Ouside nor Inside mine application, belonging to my project. And just for that reasen i assumed that Microsoft is doing this.

    The only question was: How did Microsoft do this? Fill the textbox "File Name:" with the name of the current web page? That textbox belongs to the SaveFileDialog Window. Or Screen. How do you call it? And the textbox is placed on the bottom line of that window. It is a textbox that can contain more then one line. The other textbox, who is placed under the FileName textbox, is called: "Save As". And Sam! It realy says "Save As". But this is a textbox! And can also contain more then one line. That last textbox gives the Extension of the file. For example "*.Gif" Or "*.Jpeg". And more of that extensions.

    But there is more:

    In your code you can create and activate a SaveFileDialoge, like this:

    (1)      Dim saveFileDialog1 As New SaveFileDialog() 

    (1)      saveFileDialog1.ShowDialog() 

    And you can do this:

    I have given my created WebBrowser the name: WbrWebBrowser.

    (2)   WbrBrowser.SaveFileDialog.Show()

    Both (1 And 2) SaveFileDialogs texboxes are ampty. But are not ampty when i do it with the rigth - click of the mouse, when the WebBrowser page is activated. See the story above. And i think that it has to do with the DocumentText property. Like this sample:

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
        Handles Me.Load

        webBrowser1.DocumentText = _
            "<html><body>Please enter your name:<br/>" & _
            "<input type='text' name='userName'/><br/>" & _
            "<a href='http://www.microsoft.com'>continue</a>" & _
            "</body></html>"

    End Sub

    This is Script. And that is something i have not learned yet. But it could be the reason for the filled textbox "FileName:" in the SaveFileDialog window. When i used the right-mouse click manner to get the SaveFileDialog. See above.

    If it is so, could you help me with that script? In that way that the name of the current page of my WbrBrowser came in the Filename textbox. Then everything is ready to save that file or image. Maybe it is a more easy way to get that filename.

    Sam and Reed, i hope that i have make it more clear now. Sorry for my bad Englisch grammar.

    I wish you both to have a very good weekend.

    Best regards,

    Willem.


    Saturday, July 28, 2018 12:04 PM
  • I don't think you ever stated explicitly that you were trying to save pictures. That is not a language issue or a beginner mistake. Reed said he assumes that is what you need to do and you did not state explicitly that that is correct, at least not initially.

    You can read my article Introduction to Web Site Scraping. The code is C# but you should be able to figure out the VB equivalents and the rest of the article will help.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, July 28, 2018 3:10 PM
  • Sam,

    So it came over to you. I am sorry for that. I am not trying to save pictures. But i try to save a web page name. And that name has the type *.Gif. And that is a image file. Not more and not less. And i suppose that Reed was understanding that. And so he gives the advice tot download it. When i have got the src.

    This was what he wrote:

    Once you have the correct <IMG> tag with your current code, you need to take the "src" URL in that tag and use it to make a separate call to download that image.  You can use the DownloadFile method I mentioned and give it the URL from the IMG tag along with the local file path where you want the image to be saved.

    And he gives me this:

    My.Computer.Network.DownloadFile() is one such method that you could use to pass the URL to the image file and a path and filename on the local computer where you want to save the downloaded image.

    And this Sam, says me that Reed understand what i want. I will read your Introduction to Web Site Scraping. I promis you. But first i go trying Reeds advise. Mark! This all is without using a SaveFileDialog.

    Best regards,

    Willem.

    Saturday, July 28, 2018 6:18 PM
  • I am not trying to save pictures. But i try to save a web page name. And that name has the type *.Gif. And that is a image file.

    The words "image" and "picture" are the same thing, especially in this context. Okay, I guess that if an image is a chart then it might seem wrong to call it a picture but for an img tag they are the same thing. The file that is the src attribute of an img tag is an image file that can also be a picture file. The DownloadFile method downloads a file and if that file is the src attribute of an img tag then the img tag does not differentiate between images that are charts and images that are pictures. If you write HTML then you will use the same img tag for all of that.

    Since you are downloading the contents of a file, you are not saving a web page name. You are getting a file name from the img tag. There are however very many other ways to say "getting a file name".

    To the extent that any of this miscommunication is caused by language problems, I apologize for not being more understanding.

    And if the formatting of this post is a bit off, then this forum's software is the problem.



    Sam Hobbs
    SimpleSamples.Info

    Saturday, July 28, 2018 8:01 PM
  • Hi Sam,

    You are right!  I accept your pologize. Because you show me that you do finaly understand what i want. And that is very nice. But the contens of the problem is more difficult then i thought that it was:

    1. Get the collection of the Elements Tag name("IMG"). (Easy)

    2. Open! the DomElement. (Very difficult and for this item i need help by code sample)

    3. Get the attribute IHTMImgElement_src (Easy)

    From point 2 it became difficult for me. Because i don't know how to open the DomElement. With debugging the code, i can click on the right arrow. Then he opened. But how do you do that programmatically?

    This was all what i try to ask. And now i do apologize for the fact that i used to many words for it, to explain what i want.

    Best regards, Willem.

    Wednesday, August 1, 2018 10:33 AM
  • Hi Sam,

    You are right!  I accept your pologize. Because you show me that you do finaly understand what i want. And that is very nice. But the contens of the problem is more difficult then i thought that it was:

    1. Get the collection of the Elements Tag name("IMG"). (Easy)

    2. Open! the DomElement. (Very difficult and for this item i need help by code sample)

    3. Get the attribute IHTMImgElement_src (Easy)

    From point 2 it became difficult for me. Because i don't know how to open the DomElement. With debugging the code, i can click on the right arrow. Then he opened. But how do you do that programmatically?

    This was all what i try to ask. And now i do apologize for the fact that i used to many words for it, to explain what i want.

    Best regards, Willem.

    In your item #2, I assume you mean isolating the correct IMG tag based on its location in the DOM.

    When you access WebBrowser1.Document.Body you are in the DOM.  There are properties to access Children or FirstChild as well as Sibling elements.  You can also use GetElementsByTagName to select all elements of a particular kind.

    Depending on the layout of the source HTML and what each element contains, you may be able to find the correct IMG tag based on its parent element name.  Or you might need to count children to find the correct element by "index".

    If you open the source HTML in your browser using developer or debug mode (press F12 in Edge browser) you can see the HTML DOM.  Then you can count the number of top-level elements in the body to find the one under which your target IMG tag is located.  You may have to then count how many levels deep that element is within other elements.  Once you have this "tree" of counts you can use the Children property on each element in the code to "walk" the DOM and locate the correct IMG element in your code.

    I hope that makes sense, given our language barrier issues.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, August 1, 2018 11:29 AM
    Moderator
  • Hi Reed,

    You can also use GetElementsByTagName to select all elements of a particular kind.(Your writing)

    Yes, that was what i was doing, look:

     Private Sub GetContextFromDomElement()
        Dim Elems As HtmlElementCollection
        If (WbrWebBrowser.Document IsNot Nothing) Then
          Elems = WbrWebBrowser.Document.GetElementsByTagName("IMG")
          For Each Elem As HtmlElement In Elems
    
          Next
        End If
      End Sub
      ' My.Computer.Network.DownloadFile()
    If i debug this code, i can see that there are no children . From Elem downwards are 6 places then i have DomElement. I don't know how to explain, but i have to activate or open or get or collapse the DomElement. To get the collection out of the DomElement. After that, it is not needed to count IHTMLimgElements. Because, with one command i can get the desired IHTMLImgElement_scr. And that command is GetAtrribute. But first and for all, the DomElement has to be open. I have to get the collection of IHTMLImgElements. How?

    Regards, Willem.


    Wednesday, August 1, 2018 2:41 PM
  • Getting the src attribute is easy for me. I can help you with that.

    Finding the correct img tag is not so easy, but the thing that is the difficult part varies. The solution depends on the document. Every web page is different. I have done this type of thing many times. There are many possibilities depending on the specific web page.

    The first possibility to look for is if the tag has an id. By definition of what an id is, it should be unique. They are not always unique. Assuming the id is unique, you can get the img element directly using the id.

    Something else I would do is to find a parent element with an id and start there and go down from there instead of starting at the top element, the body element.

    Many developers would just count the number of elements to get to where they need to go. I prefer to put a little more work into it and to verify that things are what I expect them to be. If the web page is changed then I think it is better to issue a meaningful error message instead of letting the program get really confused and then talk garbage about something not working. So for me, I try to check things as I go down, so for example there might be a div element and then two p elements and then an img element. I would check that the three preceding elements are those element tags.

    Getting the collection of img elements is easy but it might not be the best way to start. So first let us ensure you have that part working. Is there a more reliable way to find the img tag? I will look at the page too and post results later.



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, August 1, 2018 6:40 PM
  • Hi Willem,

    You shouldn't have to "open" anything.  The Elem variable is the IMG element.  For example, if I use the website you posted, your code, and just get the src attribute and put it into a textbox so that I can see the address, I get the following:

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WbrWebBrowser.ScriptErrorsSuppressed = True
        WbrWebBrowser.Navigate("http://www1.wetter3.de/animation.html")
    End Sub
    Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
        Dim Elems As HtmlElementCollection
        If (WbrWebBrowser.Document IsNot Nothing) Then
            Elems = WbrWebBrowser.Document.GetElementsByTagName("IMG")
            For Each Elem As HtmlElement In Elems
                Dim src = Elem.GetAttribute("src")
                RichTextBox1.AppendText(src)
                RichTextBox1.AppendText(vbCrLf)
            Next
        End If
    End Sub
    

    The contents of RichTextBox1 contain:

    http://www1.wetter3.de/Animation_12_UTC/00_1.gif
    http://www1.wetter3.de/Buttons/button_startseite_dt.png
    http://www1.wetter3.de/einpixel.jpg
    http://www1.wetter3.de/einpixel.jpg
    http://www1.wetter3.de/Buttons/button_zumTut_dt.png
    http://www1.wetter3.de/einpixel.jpg
    http://sponsorads.de/de/images/arrow_black.png
    http://www1.wetter3.de/Buttons/button1a_dt.png
    http://www1.wetter3.de/Buttons/button2_dt.png
    http://www1.wetter3.de/Buttons/button3_dt.png
    http://www1.wetter3.de/Buttons/button4_dt.png
    http://www1.wetter3.de/Buttons/button5_dt.png
    http://www1.wetter3.de/Buttons/button6_dt.png
    http://www1.wetter3.de/Buttons/button7_dt.png
    http://www1.wetter3.de/Buttons/button8_dt.png

    So the very first result appears to be the URL to the image you want to download and save.

    That being the case, you should be able to just use the first IMG tag without looping through all of them.

    One thing I discovered while testing this is that the site will throw a "forbidden" error if the web request does not contain a known user agent header.  Because of this, we can't use the DownloadFile() method.  Instead we must manually create a WebRequest and get the response.

    Here is an example that saves the map image.  Be sure to edit this code to use a valid file path on your computer:

    Dim Elems As HtmlElementCollection
    If (WbrWebBrowser.Document IsNot Nothing) Then
        Elems = WbrWebBrowser.Document.GetElementsByTagName("IMG")
        Dim targetElement = Elems.Item(0)
        Dim sourceURL = targetElement.GetAttribute("src")
        Dim request = System.Net.WebRequest.CreateHttp(sourceURL)
        request.UserAgent = "Mozilla/5.0"
        Using response = request.GetResponse
            Using img = Image.FromStream(response.GetResponseStream)
                img.Save("E:\Data\testmap.gif")
            End Using
        End Using
    End If
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by HofsteeWillem Friday, August 3, 2018 4:18 PM
    Wednesday, August 1, 2018 7:01 PM
    Moderator
  • I want without a SaveFileDialog.show() programmaticaly get that file name, and save it into a row of a Access database that has the type Appendix.

    Do you want that the user press a button after the web page is loaded or do you want it to be totally automatic and for the image to be saved without the user doing anything more? I think it is possible to make it totally automatic but that will be more work.


    Sam Hobbs
    SimpleSamples.Info

    Wednesday, August 1, 2018 8:14 PM
  • Wow Reed!

    Thank you!. I have the two  days that are past, also tried to find something. But i did not find a thing. I was walking in a "Circle" and don't came out of it. I going try out your samples. It is a bit strange of that webReguest. I have one line above my Webroser_Load() the following script:

    <PermissionSet(SecurityAction.Demand, Name:="FullTrust")>

    Has that the same meaning as WebReguest?

    Anyhow, i going tot try your samples.

    Regards and a nice weekend,

    Willem.

    Friday, August 3, 2018 4:18 PM
  • Hi Sam,

    Thank you. I will wait for your post, but Reed has found something very intresting. And i belief that it is the solution for the problem. So i try it out.

    Best regards,

    Willem.

    Friday, August 3, 2018 4:25 PM
  • Wow Reed!

    Thank you!. I have the two  days that are past, also tried to find something. But i did not find a thing. I was walking in a "Circle" and don't came out of it. I going try out your samples. It is a bit strange of that webReguest. I have one line above my Webroser_Load() the following script:

    <PermissionSet(SecurityAction.Demand, Name:="FullTrust")>

    Has that the same meaning as WebReguest?

    Anyhow, i going tot try your samples.

    Regards and a nice weekend,

    Willem.

    No, the permissionset attribute doesn't have anything to do with that.

    The webserver is looking at the headers and is configured to only respond to an HTTP GET request from a web browser agent.  Since you are just re-downloading the same image that was already requested through the WebBrowser control, we can repeat the request for the image in code by configuring our request object to mimic the browser.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, August 3, 2018 4:29 PM
    Moderator
  • Thank you. I will wait for your post, but Reed has found something very intresting. And i belief that it is the solution for the problem. So i try it out.

    I could have "found" something like that too. I was not sure what you needed and I was trying to understand what you needed. If that code works for you then I won't try to provide anything different.

    Just understand that that code will download the first image in the page. If the page is modified and some other image is in it that exists before the one you want then the program won't do what you want. Hopefully if that happens then it will be a simple matter of changing the code so it downloads the correct image.



    Sam Hobbs
    SimpleSamples.Info

    Friday, August 3, 2018 9:38 PM
  • Hoi Sam,

    The first. By pressing the Button.

    Regards, Willem.

    Monday, August 6, 2018 2:49 PM
  • Hai Sam,

    Sorry i did'nt reply your post. Your understanding is correct. It is good that if the page modified that i get a other name for img. When i take one chart out of the archiv, the src is correct. On the same way i have also the correct date. With GetAtrribute("fielupdatedDate"). And so the first part of this all has come to a succes and is finished. Thank you both for your help.

    But now part 2 starts. And that is saving the image img into a Access database. That database is called "Situatie". In Englisch "Situation". And has to do with the weather pressure situations each day. "Situatie" is part of a group which exist of 15 databases. The whole database is a related database.

    I will explain to you what Microsoft has done with Access at the time they build it:

    You know that a database consist of rows and columns. The intersection of the rows and colums is called a field. it's more a row field or a column field. Does'nt matter, i call it field. That field gets a type for the whole column. Properly speaking you can say that the column gets a type. There are a lot of choices. One off the choices is Attachment(Appendix). It stands under the header "Settings". That type Attachment can save a lot off things. Where under images. That means that it is for me the best choice. But it gives a problem, where Microsoft did'nt think about:

    The type Attachment is split up in 3 fields. 1. FileData 2. FileName 3. FileType. And the header is called to the Column name. In my situation : "Weather300". The 00 is the UTC time. Do you know what the compiler says to this when you try to save a image file? He said: "I can't save the fundamentals to a field that consist of more fundamentals". Microsoft never realised that you can't save a file to a field that contains more then one field!. How stupid of them!

    My first trying was to save it to a disk. Hardisk or DVD that doesn't matter. You know what the compiler said? He said: "A common faulth occurs.  GDI+".

    I am sorry, but when this sort off problems raised for such a simple matter, then i begin to think very bad about Microsoft. Just saving a file. Not more not less. Gives huge problems.

    Regars, Willem.


    Monday, August 6, 2018 4:14 PM
  • I am not an expert on Access and images and databases but I think that the experts recommend saving images as files separate from the database and to just save a filename in the database. If the files are all saved in the same directory then you don't need to save the directory in the database. If the filename can be determined from other data, such as a time that is saved in the database, then it might not be necessary to put anything more in the database.

    Generally, when images are saved in a database, they are saved as Binary Large OBjects (BLOBs) but I don't see an Access data type for that. Apparently the OLE Object and the Attachment types are designed for that. If you must store the image in the database then you should look in an Access forum for answers and if you don't find answers then ask a question in a forum for Access.



    Sam Hobbs
    SimpleSamples.Info

    Monday, August 6, 2018 4:34 PM
  • Thank you Sam, I will do that. But i do consider to try to save img into a string file. Like this:

    img.Save(m_img As String). And then i save m_img into a Long Text Typed field. Maybe it works.

    Willem.



    Monday, August 6, 2018 6:32 PM