Results 1 to 16 of 16

Thread: Webbrowser control....control...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Manchester
    Posts
    266

    Arrow Webbrowser control....control...

    Hey Guys and Girls,

    It's been a while since I have posted here but I have yet again been bitten by the VB bug and have a project in the pipeline.

    Now, here is my problem.

    Lets say there is a webpage. I know that at a certain location on said webpage there is an image. The location of that image will never change and neither will the size or scale. Is it possible to use the web browser control to load the webpage and display, or show only the image and disabled the ability to look at the rest of the webpage. In other words, I want the web browser control to fit neatly around the image.

    Does the webbrowser control have the ability to download on element of a webpage (the image) OR download the whole webpage but only show the webpage.

    As always thanks alot and rep to everyone who offers suggestions.

    Jord

  2. #2
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Webbrowser control....control...

    Let's say you are correct that the actual position of the image on the page will never change. But it could.

    Anyway, put your WebBrowser inside a Picturebox control and by trial and error just move the WebBrowser up and to the left until only the top and left edge of the image shows. Now change the height and width of the Picturebox until only the bottom and right edge of the image shows.

    If all you are interested in is the image there are several APIs that you can use to download the image only. One API is this:
    Code:
    Private Declare Function URLDownloadToFile Lib "urlmon" _
     (ByVal pCaller As Long, _
      ByVal szURL As String, _
      ByVal szFileName As String, _
      ByVal dwReserved As Long, _
      ByVal lpfnCB As Long) As Long
       
    Private Const ERROR_SUCCESS As Long = 0
    Private Const BINDF_GETNEWESTVERSION As Long = &H10
    Private Const INTERNET_FLAG_RELOAD As Long = &H80000000
      '
      '
      '
     '
     ' Download the file. BINDF_GETNEWESTVERSION forces
     ' the API to download from the specified source.
     ' Passing 0& as dwReserved causes the locally-cached
     ' copy to be downloaded, if available. If the API
     ' returns ERROR_SUCCESS (0), DownloadFile returns True.
     '
     DownloadFile = URLDownloadToFile 0&, SourceUrl, LocalFile, BINDF_GETNEWESTVERSION, 0&
    but you will need to then use LoadPicture to get the image into a Picturebox
    Last edited by jmsrickland; May 20th, 2009 at 02:47 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  3. #3
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Webbrowser control....control...

    >Lets say there is a webpage. I know that at a certain location on said webpage there is an image. The location of that image will never change and neither will the size or scale. Is it possible to use the web browser control to load the webpage and display, or show only the image and disabled the ability to look at the rest of the webpage. In other words, I want the web browser control to fit neatly around the image.

    This is a bit more complicated than moving a webbrowser control around in a picturebox but it is possible with several different technologies, which I will get into in a moment.

    >Does the webbrowser control have the ability to download on element of a webpage (the image) OR download the whole webpage but only show the webpage.

    Yes, but not directly... One way, is to tell IE via it's settings and options that you don't want it to download images and you don't want image placeholders but you will either have to figure out what and where those setting are to do it via a program or you will have to do it through IE. BUT, with trying to use that as a solution you will only have half of your solution and no way to reverse it, meaning no text image only.

    Another solution is to use one of the following technoligies...
    URLDownloadToFile API
    Add a reference to Microsoft HTML Object Library
    Use an INet control
    And more...

    Now, my suggestion is to use the Microsoft HTML Object Library and do something like...
    Code:
    Dim HtmlDoc As HTMLDocument, C As Variant 'or C As Object
    Set HtmlDoc = WB.Document 'webbrowser control
    That is, use the above after you have navigated and fully loaded the web page in a webbrowser control that is not visable. Then use something like...
    Code:
    For Each C In WB.Document.images
    then once you have a reference to the image you can use something like...
    Code:
    SomeStringVariable = C.OuterHTML
    Then use SomeStringVariable as what you navigate to with a visable webbrowser control and inversely you can use...
    Code:
    ImageUrl = C.HREF
    and use that to navigate to with your visable webbrowser control. Now, with the above you have the pieces to either display the text only or the image only of a web page but as a warning if there is more than one image on the page this will only exclude one image.

    Now, here is just a sample of a solution that I created to strip specific elements from a web page and there might be elemets of the program missing...
    Code:
    'status code for getting the page successfully.
    Public Const HttpStatusOK200 As Integer = 200
    
    'status code for not getting the page
    Public Const HttpFileNotFound404 As Integer = 404
    
    'status code for request timed out.
    Public Const HttpTimeOutError12002 As Integer = 12002
    
    Public Type HTMLProp
      MetaTagName As New Collection
      MataTagContent As New Collection
      ImagesFoundInPage As New Collection
      ImagesAltText As New Collection
      LinksInSite As New Collection
      LinkInSiteTitle As New Collection
      LinksToOtherSites As New Collection
      ScriptFunctions As New Collection
      Text As String
      PathToHTMLFile As String
      Title As String
    End Type
    
    Public Function ParseHTMLFile(W As WebBrowser) As HTMLProp
    
    On Error GoTo ParseHTMLFileError
    
    Dim MetaTag As Object, MetaTagCollection As Object
    Dim HtmlDoc As HTMLDocument, C As Variant, HREFBase As String, I As Double
    
    HREFBase = GetHttpBase(W.LocationURL)
    
    Set HtmlDoc = W.Document
    Set MetaTagCollection = HtmlDoc.getElementsByTagName("meta")
    
    For Each MetaTag In MetaTagCollection
      ParseHTMLFile.MataTagContent.Add MetaTag.content
      ParseHTMLFile.MetaTagName.Add MetaTag.Name
    Next
    
    For Each C In W.Document.images
      ParseHTMLFile.ImagesFoundInPage.Add C.href
      ParseHTMLFile.ImagesAltText.Add C.alt
    Next
    
    For Each C In W.Document.links
      If InStr(1, C.href, HREFBase) > 0 Then
        ParseHTMLFile.LinksInSite.Add C.href
        ParseHTMLFile.LinkInSiteTitle.Add C.innerText
      Else
        ParseHTMLFile.LinksToOtherSites.Add C.href
      End If
    Next
    
    For I = 0 To W.Document.scripts.length - 1
      ParseHTMLFile.ScriptFunctions.Add W.Document.scripts.Item(I).outerHTML
    Next I
    
    ParseHTMLFile.Text = W.Document.body.innerText
    ParseHTMLFile.Title = W.Document.Title
    ParseHTMLFile.PathToHTMLFile = GetHtmlFile(W.LocationURL)
    
    Exit Function
    ParseHTMLFileError:
    
    MsgBox Err.Description
    Resume Next
    End Function
    
    Public Function GetHttpBase(PathFile As String) As String
    
    On Error GoTo GetHttpBaseError
    
    Dim P As Double
    'http://
    P = InStr(Len("http://") + 1, PathFile, "/")
    If P > 0 Then
      GetHttpBase = Left(PathFile, P)
    Else
      GetHttpBase = PathFile & "/"
    End If
    
    Exit Function
    GetHttpBaseError:
    
    MsgBox Err.Description
    
    End Function
    Now a couple of notes about the above...

    It is old code that worked on 9x, yes I use collections in the above example and did not eat the donuts because of it... Also, Project that this came from I also have many more references than noted above but the only two that you may need are Microsoft XML vX.? (I used 3 at the time) and Microsoft ActiveX Data Objects 2.6 Library (but don't think so as I do not think any of the variable declarations above reference either of those libraries.)

    Good Luck
    Option Explicit should not be an Option!

  4. #4
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Webbrowser control....control...

    This is a bit more complicated than moving a webbrowser control around in a picturebox

    Really? Explain why you say that? This is one of the easiest and simpliest ways to do it. Of course, the image cannot ever have a different position but that was the one restriction as I pointed out. Also, if he already has the URL to the image then why go through all that stuff you posted when just a simple image download will do. Do you know something that I don't?
    Last edited by jmsrickland; May 21st, 2009 at 12:56 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  5. #5
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Webbrowser control....control...

    >This is a bit more complicated than moving a webbrowser control around in a picturebox

    >Really? Explain why you say that?

    OP's original requirements. e.g. only image, only text...

    >This is one of the easiest and simpliest ways to do it. Of course, the image cannot ever have a different position but that was the one restriction as I pointed out.

    As did the OP but you did not address the second part of the requirements. No picture only text...

    >Also, if he already has the URL to the image then why go through all that stuff you posted when just a simple image download will do. Do you know something that I don't?

    OP did not state that they had the URL of the image. Hence my suggestion on how to find the URL and use only it to navigate or to remove it to get only the text. Hence my statement...

    >This is a bit more complicated than moving a webbrowser control around in a picturebox

    Sorry if I offended you James. May I call you James? But while your solution will work for half of the OP's requirements, it does not and will not work for the other half, and if the website ever changes something on the web page that the OP is accessing, well that could break your solution also.

    Once again, sorry if I offended you sir.
    Option Explicit should not be an Option!

  6. #6
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Webbrowser control....control...

    What do you mean only half of the OP's requirements? His entire requirements is this:

    Now, here is my problem.

    Lets say there is a webpage. I know that at a certain location on said webpage there is an image. The location of that image will never change and neither will the size or scale. Is it possible to use the web browser control to load the webpage and display, or show only the image and disabled the ability to look at the rest of the webpage. In other words, I want the web browser control to fit neatly around the image.


    So where is it stated that there is more to his requiremnts? Putting the browser inside a picturebox solves this requirement? It shows only the image; isn't that what he wants? And didn't I also say that it required that the image never change it's position which he seems to be sure it wont but then that is his problem but the picturebox method works for the contents of his request. If the image does change positions then he can always re-adjust the browser in the picturebox. I'm not saying this is the best solution but it is the quickest and simpliest.

    My argument is not that your method may or may not be the ultimate solution, and in the long run it probably will be but it is an argument to say it is a bit more complicated than moving a webbrowser control around in a picturebox again going on the basis that the image position will not change; which is the key factor here. He says it wont; then so be it. You and I say it could.

    Also I said if he already has the URL to the image......

    which I know he didn't say he did but if he did have it......

    then he only needs to use the API. This was only a suggestion in the event he does have it.

    And yes, James is nice, thank you
    Last edited by jmsrickland; May 21st, 2009 at 10:40 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  7. #7
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Webbrowser control....control...

    >What do you mean only half of the OP's requirements? His entire requirements is this:...

    the next sentance sir....

    >Does the webbrowser control have the ability to download on element of a webpage (the image) OR download the whole webpage but only show the webpage.
    Option Explicit should not be an Option!

  8. #8
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Webbrowser control....control...

    Well, that to me was just a secondary question but the first part was his want. OK, for the sake of argument I'll give you that one.

    I think he will probably go for your suggestion however just because it solves the problem in the long run.
    Last edited by jmsrickland; May 22nd, 2009 at 12:01 AM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  9. #9
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Webbrowser control....control...

    Perhaps, perhaps not, maybe we shall see if OP ever comes back to this thread...

    If the user does know the URL to the picture in question then Hypetia's solution in this thread...

    http://www.tek-tips.com/viewthread.cfm?qid=708828

    about 3/4's of the way down would only need a picture box but the entire thread is worth reading as there is an example by strongm with the inet control.
    Option Explicit should not be an Option!

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Webbrowser control....control...

    Here is something I found that works great as long as the image tag has a name:
    Code:
      '
      '
      Dim O As Object
    
      Set O = WebBrowser1.Document.body.createControlRange()
    
      '
      ' Example image tag
      '
      ' <img name="myimage" src=http://www.some-website.com/the_image.jpg>
      '  
      Call O.Add(WebBrowser1.Document.All("myimage"))
      
      Call O.execCommand("Copy")
      
      Set Picture1.Picture = Clipboard.GetData
      '
      '
    What I don't know is how to use this if the image tag does not have a name parameter.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Webbrowser control....control...

    Hmmm...

    Wonder if it would work with alt text???

    Where did you find that? I'd like to see the entire code/notes about project...
    Option Explicit should not be an Option!

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Webbrowser control....control...

    It was just some old code I had from way back. I have no idea from where I got the original but what I posted is all that I had in the little sample program. Pretty cool, huh?


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  13. #13
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Webbrowser control....control...

    >What I don't know is how to use this if the image tag does not have a name parameter

    Just had a thought...

    If you use...

    Code:
    For Each C In W.Document.images
    and then modify what you have, somehow, to set it equal to one of the images (or say a picbox array for each image...) then that might solve your quandry(sp)...

    And, yeah it is cool but if image is anything other than bitmap then one would have to jump through another set of hoops if the image was to be saved or just use the API as we have mentioned.

    Good Luck
    Option Explicit should not be an Option!

  14. #14
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Webbrowser control....control...

    but if image is anything other than bitmap

    That is always the case; image is anything other than bitmap.

    The more I think about it the more I think I got that code from here but still that was all there was from the beginning anyway no matter where I got it.

    This is the part that bugs me....

    Call O.Add(WebBrowser1.Document.All("myimage"))

    ....kind of like when you are trying to click on a named button. I was trying to think how to do this in the same fashion of how you would go about clicking on a nameless button. I've got nothing.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Manchester
    Posts
    266

    Re: Webbrowser control....control...

    Thanks guys. I appreciate your input.
    I've read through everything and it has been a good help.
    However, something I failed to mention is that the image file is generated by Javascript into a PNG file making things difficult. Furhermore, the directory in which the image is stored is a hidden

    What I ended up doing is having the IE WebBrowser plugin write it's own page, into which I embedded the page on which the PNG file was shown. In this way I could write the HTML myself and so place the image as if I had written the web page myself. This seems to work, even when the page changes location.

    Just a quick note aswell, the software I am developing will be going on sale at some point later this year and I'd be quite happy to offer you fellas' a free copy as you have been helpful. I can't really go into massive detail and, the software may or may not be of interest to you. If either of you are interested in the stock market or daytrading then it will !

    Thanks for all the help guys. If you do want a copy when it is released, pop your email address into a PM and i'll keep you updated.

    Thanks guys.

    Jord

  16. #16
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: Webbrowser control....control...

    James,...

    If I were the opposite sex, I think you would offer to kiss me... BUT I will take the high five instead...
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    WB.Navigate "http:\\www.yahoo.com"
    End Sub
    
    Private Sub Command1_Click()
    Dim O As Object
    Dim Images As Variant
    
    Set O = WB.Document.body.createControlRange()
    
    For Each Images In WB.Document.Images
      O.Add Images
      O.execcommand "Copy"
      Picture1.Picture = Clipboard.GetData
    Next Images
    
    End Sub
    Add webbrowser control and rename to WB. Add picture box. Add Command Button. Add the code above and set break point on the command1_click event. Run and let the page load. Then click and walk through...

    intraman,

    Don't know of a site that has *.png graphics but this should work for you also...

    Good Luck
    Option Explicit should not be an Option!

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