1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to save captcha image to picturebox with webbrowser (not webrequest)

Discussion in 'Visual Basic .NET' started by m3ownz, Feb 11, 2011.

  1. m3ownz

    m3ownz Regular Member

    Joined:
    Dec 12, 2009
    Messages:
    311
    Likes Received:
    135
    Hello

    Having spent the last few weeks getting up to speed with webrequest, I am now working on a project that requires a webbrowser due to huge and complicated amounts of javascript!
    Also for this project visual progress is a must, and speed is less important, so it really needs to use a webbrowser (sorry kittens)

    I can get it to do most things, but im stuck when it comes to saving captcha images (for manual entry or decaptcha service)
    As they are dynamic, i can NOT just make a webrequest to the image url, as it will return a new image.

    I found a solution in VB6:

    HTML:
          Dim O as Object
    
          Set O = WebBrowser1.Document.body.createControlRange()
    
          Call O.Add(WebBrowser1.Document.All("verificationImg"))
    
          Call O.execCommand("Copy")
    
          Set Picture1.Picture = Clipboard.GetData 
    
    But this doesnt work in .net as createControlRange is not a member of Forms.Htmlelement

    Googleing 'createControlRange for vb.net' or similar just brings up a few unresovled forum posts by people asking the same question.

    Now im sure this is possible, as i have seen .net programs that use a browser and display the captcha in an overlay, but i am stuck.
    I can see the bloody image on the screen in the webbrowser, and can right click and select 'save', so it must be possible to do it programatically!

    Any help appreciated.
     
    Last edited: Feb 11, 2011
  2. Kuranyi

    Kuranyi Junior Member

    Joined:
    Dec 21, 2009
    Messages:
    104
    Likes Received:
    51
    Hello,
    i found this code snippet a long time ago:
    Code:
    Dim doc As MSHTML.IHTMLDocument2 = DirectCast(Browser.Document.DomDocument, MSHTML.IHTMLDocument2)
                Dim sobj As MSHTML.IHTMLSelectionObject = doc.selection
                Dim body As MSHTML.HTMLBody = TryCast(doc.body, MSHTML.HTMLBody)
                Dim range As MSHTML.IHTMLControlRange = TryCast(body.createControlRange(), MSHTML.IHTMLControlRange)
                
                Dim pic As MSHTML.IHTMLControlElement = DirectCast([I]BildElement[/I].DomElement, MSHTML.IHTMLControlElement)
                
                range.add(pic)
                range.[select]()
                range.execCommand("Copy", False, Nothing)
                Clipboard.GetImage().Save("captcha.jpg")
    
    BildElement is the htmlElement of the picture.
    I hope that helps.
     
    • Thanks Thanks x 1
  3. DarkoOS

    DarkoOS Newbie

    Joined:
    Feb 8, 2010
    Messages:
    19
    Likes Received:
    6
    Occupation:
    IT Manager
    Location:
    Osijek, Croatia
    Home Page:
    Hell,

    code that i use in VB, works perfect on 16 torrent sites. I use AxWebBrowser controll.

    Code:
            Dim doc As mshtml.IHTMLDocument2 = DirectCast(WebBrowser1.Document, mshtml.IHTMLDocument2)
            Dim sobj As mshtml.IHTMLSelectionObject = doc.selection
            Dim body As mshtml.HTMLBody = TryCast(doc.body, mshtml.HTMLBody)
            sobj.empty()
            Dim range As mshtml.IHTMLControlRange = TryCast(body.createControlRange(), mshtml.IHTMLControlRange)
            Dim img As mshtml.IHTMLControlElement = DirectCast(WebBrowser1.Document.Images(broj), mshtml.IHTMLControlElement)
            range.add(img)
            range.[select]()
            range.execCommand("Copy", False, Nothing)
            Dim bimg As New Bitmap(Clipboard.GetImage())
            Me.PictureCaptcha.Image = bimg
            Me.PictureCaptcha.Image.Save(ime, System.Drawing.Imaging.ImageFormat.Jpeg)
    
     
    • Thanks Thanks x 1
  4. simpleonlinetest

    simpleonlinetest Regular Member

    Joined:
    Feb 18, 2010
    Messages:
    208
    Likes Received:
    25
    I had a similar issue not to long ago...i ended up using 2 forms....on the second for I would pass the picture to a picture box on form 2 in the picture box and code up a text box and a button and your set..
     
  5. shudogg

    shudogg Regular Member

    Joined:
    Sep 23, 2008
    Messages:
    412
    Likes Received:
    153
    Occupation:
    Internet Marketing
    Location:
    Indiana
    Home Page:
    Use WebRequest not a WebBrowser. I am so sick of seeing these amateur skids making "bots" using Web Browsers..

    Spend a few days, make some "test" programs for yourself to learn with. Follow tutorials, example code, and source code snippets.. and learn REAL programming.

    WebBrowsers are the sissy way out.