|
-
Oct 28th, 2009, 08:21 PM
#1
Thread Starter
Fanatic Member
Copying image from website
Code:
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
This code will do it
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?
-
Oct 28th, 2009, 08:57 PM
#2
Re: Copying image from website
One way could be to do like this:
Add a WebBrowser control to your form.
You can load the image by using the Naviagate method
vb.net Code:
WebBrowser1.Navigate ("https://mail.google.com/mail/help/images/logo2.gif")
Then in the DocumentComplete event, do whatever you want to do with this image.
-
Oct 28th, 2009, 09:06 PM
#3
Thread Starter
Fanatic Member
Re: Copying image from website
That's a good plan. I am thinking of using shdocvw.internetexplorer object actually.
Can I do that? How different webbrowser control is compared to shdocvw.internetexplorer?
-
Oct 28th, 2009, 10:42 PM
#4
Addicted Member
Re: Copying image from website
This works
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("https://mail.google.com/mail/help/images/logo2.gif")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim theElementCollection As HtmlElementCollection = Me.WebBrowser1.Document.GetElementsByTagName("img")
Dim n As Int32 = theElementCollection.Count
Dim index As Int32 = 1
For Each curElement As HtmlElement In theElementCollection
'MessageBox.Show(curElement.GetAttribute("src").ToString)
Dim imgURL As String = curElement.GetAttribute("src").ToString
Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(imgURL)
Dim response As System.Net.HttpWebResponse = request.GetResponse()
Dim stream As System.IO.Stream = response.GetResponseStream()
' Get the length of the content
Dim length As Integer = response.ContentLength
Dim bytes(length) As Byte
For i As Integer = 0 To length - 1
bytes(i) = stream.ReadByte()
Application.DoEvents()
Next
' Save each image in turn.
Using output As IO.Stream = System.IO.File.Create("c:\Image" + index.ToString + ".gif")
output.Write(bytes, 0, bytes.Length)
End Using
index += 1
Next
End Sub
-
Oct 29th, 2009, 04:22 AM
#5
Thread Starter
Fanatic Member
Re: Copying image from website
Many you pretty much do my work. Thanks a lot. But I can't rate you anymore.
I am thinking of a way to do it without opening the image url separately. But it's okay. That one is useful too.
Oh ya the image must be dowlonaded through whatever proxy is on in the internet explorer object. So just knowing the url and download the image from back door wouldn't work. I already know a way to do that actually.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|