Results 1 to 5 of 5

Thread: Copying image from website

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    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?

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    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:
    1. 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.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    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?

  4. #4
    Addicted Member Smartacus's Avatar
    Join Date
    Oct 2009
    Location
    Deep South, USA
    Posts
    196

    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
    ***************************************************
    Smartacus comes packaged "As Is With No Warranty"

    ************* Useful Links ******************
    FAQs: Index / Database Development / .NET CodeBank /
    Before Posting Here...MSDN

    MZTools (I love this tool when using VB6 - Free) /

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2007
    Posts
    544

    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
  •  



Click Here to Expand Forum to Full Width