'* t=0 默认为 im 传入的是图片 id 属性
'* t=1 为 Alt 属性 t=2 为 Src 属性
Private Function GetCode(ByVal wb As WebBrowser, ByVal im As String, Optional ByVal t As Integer = 0) As Image
Try
Dim doc As HTMLDocument = DirectCast(wb.Document.DomDocument, HTMLDocument)
Dim body As HTMLBody = DirectCast(doc.body, HTMLBody)
Dim rang As IHTMLControlRange = DirectCast(body.createControlRange(), IHTMLControlRange)
Dim img As IHTMLControlElement
If t = 0 Then
img = DirectCast(wb.Document.All(im).DomElement, IHTMLControlElement)
Else
img = GetPicElement(wb, im, t)
End If
If img Is Nothing Then
Return Nothing
End If
rang.add(img)
Clipboard.Clear()
rang.execCommand("Copy", False, Nothing)
doc = Nothing
body = Nothing
rang = Nothing
img = Nothing
If Clipboard.ContainsImage Then
Return Clipboard.GetImage()
Else
Return Nothing
End If
Catch exp As Exception
Return Nothing
End Try
End Function
'* 获取网页中的某一个图片元素
Private Function GetPicElement(ByVal wb As WebBrowser, ByVal im As String, ByVal t As Integer) As IHTMLControlElement
For Each image As HtmlElement In wb.Document.Images
Dim img As IHTMLImgElement = DirectCast(image.DomElement, IHTMLImgElement)
If t = 1 Then
If img.alt.Contains(im) Then
Return DirectCast(image.DomElement, IHTMLControlElement)
End If
Else
If img.src.Contains(im) Then
Return DirectCast(image.DomElement, IHTMLControlElement)
End If
End If
Next image
Return Nothing '* 没有找到元素
End Function