None of these codes will work as everytime you load the links it will generate a new image. In this case however, it seems to generate the same letters but in a different pattern I guess. I had a problem with ReCaptcha where it'd generate a new image all the time and here is how to get the image object with the usage of a web browser control:
[highlight=vb.net]'You must add a reference to Microsoft.mshtml from the .NET tab
'If you are smart, you would know what goes in the stars. MPGH seems to be censoring some words.
Imports System****ntime.InteropServices
<ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown ), Guid("3050F669-98B5-11CF-BB82-00AA00BDCE0B")> _
Interface IHTMLElementRenderFixed
Sub DrawToDC(ByVal hdc As IntPtr)
Sub SetDocumentPrinter(<MarshalAs(UnmanagedType.BStr)> ByVal bstrPrinterName As String, ByVal hdc As IntPtr)
End Interface
Private Function GetCaptchaImage(ByRef wb As WebBrowser, ByVal captchaSubString As String, ByRef imageLocation As String, ByRef foundCaptchaImage As Boolean) As Image
For Each imgElem As HtmlElement In WebBrowser1.Document.Images
Dim currentImageURL As String = imgElem.GetAttribute("src")
If currentImageURL.Contains(captchaSubString) Then
Dim bmp As Image = HTMLElementToImage(imgElem)
imageLocation = currentImageURL
foundCaptchaImage = True
Return bmp
End If
Next
foundCaptchaImage = False
Return Nothing
End Function
Private Function HTMLElementToImage(ByVal elem As HtmlElement) As Image
Dim imgElem As mshtml.IHTMLImgElement = CType(elem.DomElement, mshtml.IHTMLImgElement)
Dim imgRender As IHTMLElementRenderFixed = CType(imgElem, IHTMLElementRenderFixed)
Dim bmp As New Bitmap(imgElem.width, imgElem.height)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim hdc As IntPtr = g.GetHdc
imgRender.DrawToDC(hdc)
g.ReleaseHdc(hdc)
g.Dispose()
Return bmp
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim captchaSucess As Boolean
Dim imageLocation As String = String.Empty
Dim captchaImage As Image = GetCaptchaImage(WebBrowser1, "recaptcha/api", imageLocation, captchaSucess)
If captchaSucess = False Then
MsgBox("Captcha image could not be found!")
Else
MsgBox("Captcha image found: " & imageLocation)
PictureBox1.Image = captchaImage
End If
End Sub[/highlight]
It should be self explanatory.
wb is the reference to your
WebBrowser control.
The
captchaSubString is a
String which contains the
substring of the image link. In this example, all ReCaptcha captcha image links contained '
recpatcha/api' in all of them so I used that there. In your case it'd probably be:
'captcha/?n=register&t=' but you can use whatever you want.
imageLocation is a reference to a
String where the image link location will be stored after the function has completed.
foundCaptchaImage is a reference to a
Boolean where a value which tells whether the function suceeded or not which will be stored after the function has completed.
Why this code is different is because it reads the image links dynamically from what is being seen on the document. I'm still trying to find a way to directly convert the
HTMLElement to an
Image without having to download the image from the 'src' value.
EDIT - Found a way, code updated. This will convert the image element seen on screen DIRECTLY ensuring that the image matches the one seen and not something that is different. Spent ages looking for methods and found one example in managed C++ but it was pretty easy to convert. I've split the code into two parts so the
HTMLElementToImage function can be reused for you guys. Just make sure the element is actually an image otherwise it'd screw up. You can use it like this and it will return the
Image:
[highlight=vb.net]Dim someLogo As Image = HTMLElementToImage(WebBrowser1.Document.GetElement ByID("some-image"))[/highlight]
If you use this code, please give credits to me and ildjarn. I know this uses unsafe code @
Chooka so don't rage at me.
