5

Is it possible to save images using Visual Basic 2008 from URL to my PC?

For example : From www.domain.com/image.jpg to C:\folder\image.jpg

P.S: I need simpliest example of the code, then I will edit is as I need.

Thanks.

Update : I want to know when the code have finished downloading of the image.

John
  • 7,500
  • 16
  • 62
  • 95
  • Check this question: [http://stackoverflow.com/questions/3615800/download-image-from-the-site-in-net-c][1] The same classes are available for VB [1]: http://stackoverflow.com/questions/3615800/download-image-from-the-site-in-net-c – Pepe Mar 13 '12 at 17:38
  • Check out the [HttpWebRequest](http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx) / [HttpWebResponse](http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.aspx) classes, or [WebClient](http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx). – Sam Axe Mar 13 '12 at 17:38

4 Answers4

15

This is the simplest way I know.

Dim Client as new WebClient
Client.DownloadFile(Source, Destination)
Client.Dispose

This is superior to using the My.Computer.Network.DownloadFile method per Microsoft's documentation

"The DownloadFile method does not send optional HTTP headers. Some servers may return 500 (Internal Server Error) if the optional user agent header is missing. To send optional headers, you must construct a request using the WebClient class."

UnhandledExcepSean
  • 12,504
  • 2
  • 35
  • 51
  • looks really easy =) It will wait image to be downloaded and the will execute next code, or I will only send request and will execute next code? – John Mar 13 '12 at 17:41
  • 1
    It is synchronous, so it will wait until the file is downloaded before continuing. – UnhandledExcepSean Mar 13 '12 at 17:45
  • You should use "Using". Your code does the same, but it's much "safer" as it directly shows you that you haven't forgotten anything. – tmighty Oct 15 '17 at 16:47
2

There's a simpler way:

My.Computer.Network.DownloadFile(Source, Desination)
user3102516
  • 97
  • 11
0

Here what i came up with.

 Public Function getImgFrmUrl(ByVal url As String, ByVal Optional ImageName As String = "", ByVal Optional DstntnPath As String = "c:\") As String

        Dim imgPath = DstntnPath & "\"
        Dim name = IIf(ImageName.Length = 0, Guid.NewGuid.ToString, ImageName)
        Dim fileExt = Path.GetExtension(url)

        Using webClient As WebClient = New WebClient
            Const _Tls12 As SslProtocols = CType(&HC00, SslProtocols)
            Const Tls12 As SecurityProtocolType = CType(_Tls12, SecurityProtocolType)
            ServicePointManager.SecurityProtocol = Tls12
            Dim data As Byte() = webClient.DownloadData(url)
            If File.Exists(imgPath + name & fileExt) Then File.Delete(imgPath + name & fileExt)

            Using mem = New MemoryStream(data)

                Using yourImage = Image.FromStream(mem)

                    If fileExt.ToLower Is ".png" Then
                        yourImage.Save(imgPath + name & fileExt, ImageFormat.Png)
                    Else
                        yourImage.Save(imgPath + name & fileExt, ImageFormat.Jpeg)
                    End If
                End Using
            End Using
        End Using

        Return imgPath & name & fileExt
    End Function
Ad Kahn
  • 551
  • 4
  • 6
0

create a module and use this function

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long

Public Function DownloadFile(sURLFile As String, sLocalFilename As String) As Boolean Dim lRetVal As Long lRetVal = URLDownloadToFile(0, sURLFile, sLocalFilename, 0, 0) If lRetVal = 0 Then DownloadFile = True End Function