vb.net captcha using WebBowser & WebClient

I need to login a page which uses captcha image, what I do is:

1- Download the captcha image using WebClient into memory.

2- Do OCR to the image and extract the code.

3- Post the login details and the code using WebBrowser.

Example:

WebBrowser1.Navigate("URL", "", paramInByte, "Content-Type:application/x-www-form-urlencoded")

I think the WebBrowser requires another captcha image before login and I get wrong code error. It was working a few months ago so I think they changed the site. How can I solve this issue? I might need to pass WebClient's cookies, headers, queries etc to the WebBrowser, but I don't know how to communicate between them.

Answers


I found this (w is the WebBrowser Object);

First I set the WebBrowser size to the captcha image size, because the code below will take the visible part in the WebBrowser, but the WebBrowser itself doesn't have to be visible.

'scroll to the picture, so we have a WebBrowser object just like a picture box
For Each i As HtmlElement In w.Document.GetElementsByTagName("img")
    If i.GetAttribute("src").Contains("here string to identify captcha image") Then
        i.ScrollIntoView(True)
    End If
Next

'Create bitmap
Dim bmp As New Bitmap(w.Width, w.Height, Imaging.PixelFormat.Format32bppArgb)
Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(bmp)
Dim hdc As IntPtr = g.GetHdc

'Do the Drawing
Dim pUnk As IntPtr = System.Runtime.InteropServices.Marshal.GetIUnknownForObject(w.ActiveXInstance)
OLE32.OleDraw(pUnk, 1, hdc, New Rectangle(0, 0, w.Width, w.Height))
System.Runtime.InteropServices.Marshal.Release(pUnk)

'Release DC and dispose
g.ReleaseHdc(hdc)
g.Dispose()

Also OLE32 Class should be declared

Public Class OLE32
    Public Declare Function OleDraw Lib "ole32.dll" (ByVal pUnk As IntPtr, ByVal dwAspect As Integer, ByVal hdcDraw As IntPtr, ByRef lprcBounds As Rectangle) As Integer
End Class

This way I can get the controls content as bmp and do OCR without requesting new page.


Need Your Help

Confused on jquery .ajax .done() function

jquery ajax

I'm a bit confused on how to use the ajax .done() function. I'm working on validating a form to check if a user exists in database and though ajax would be the best approach (still learning it). I ...

How do you iterate through menus in AppleScript?

loops safari iterator applescript web-inspector

Specifically, when I'm debugging web stuff in iOS, I can start Safari Web Inspectors by clicking on the following menu items in the Safari menu bar:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.