save images from url

前端 未结 4 1019
無奈伤痛
無奈伤痛 2021-01-13 19:27

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

For example : From www.domain.com/image.jpg to <

相关标签:
4条回答
  • 2021-01-13 20:01

    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

    0 讨论(0)
  • 2021-01-13 20:03

    There's a simpler way:

    My.Computer.Network.DownloadFile(Source, Desination)
    
    0 讨论(0)
  • 2021-01-13 20:13

    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."

    0 讨论(0)
  • 2021-01-13 20:24

    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
    
    0 讨论(0)
提交回复
热议问题