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.
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.
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.