This code will do itCode:Public Function imgToFile(ByRef img As mshtml.HTMLImg) As String Dim ocr As mshtml.IHTMLControlRange Dim theFile As String Dim trying As Integer Dim tryingResult As Boolean theFile = Trim(CStr(GetTickCount Mod 100000)) & ".jpg" 'Set img = ias.findAnElement(dw1.body, "src", "https://mail.google.com/mail/help/images/logo2.gif") 'UPGRADE_WARNING: Couldn't resolve default property of object img.Document.body. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"' ocr = img.Document.body.createControlRange For trying = 1 To 5 tryingResult = True My.Computer.Clipboard.Clear() Try ocr.add(img) Catch ex As Exception LogEvents("Failed to add image to control range") imgToFile = "" tryingResult = False End Try If tryingResult Then Try ocr.execCommandShowHelp("Copy") Catch ex As Exception LogEvents("Failed to copy image") imgToFile = "" tryingResult = False End Try End If If tryingResult Then Try My.Computer.Clipboard.GetImage().Save(theFile) Catch ex As Exception LogEvents("Failed to copy image") imgToFile = "" tryingResult = False End Try End If If tryingResult Then If FileLengthIsbad(theFile) Then tryingResult = False LogEvents("File Length is Baaaaaad") End If End If If tryingResult Then Exit For End If Sleep(5000) Next trying If tryingResult = False Then LogEvents("Failed after 5 tries") End If imgToFile = theFile End Function
The only problem is once in a while it fails. If it fails, we'll wait 5 seconds and try again. Well, it doesn't help. Once it fails it keep failing.
Is there a more reliable way to do it in vb.net?




Reply With Quote