我找到了这个(w 是 WebBrowser 对象);
首先,我将 WebBrowser 大小设置为验证码图像大小,因为下面的代码将占据 WebBrowser 中的可见部分,但 WebBrowser 本身不必是可见的。
'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()
还应声明 OLE32 类
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
这样我就可以将控件内容作为 bmp 并进行 OCR 而无需请求新页面。