【问题标题】:save images from url从 url 保存图像
【发布时间】:2012-03-30 03:40:43
【问题描述】:

是否可以使用Visual Basic 2008 从 URL 将图像保存到我的 PC?

例如:从www.domain.com/image.jpgC:\folder\image.jpg

P.S:我需要最简单的代码示例,然后我将根据需要进行编辑。

谢谢。

更新:我想知道代码何时完成图像下载。

【问题讨论】:

标签: vb.net visual-studio-2008


【解决方案1】:

创建一个模块并使用这个函数

公开声明函数 ShellExecute Lib "shell32.dll" 别名 "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

公共函数 DownloadFile(sURLFile As String, sLocalFilename As String) As Boolean 调暗 lRetVal 只要 lRetVal = URLDownloadToFile(0, sURLFile, sLocalFilename, 0, 0) 如果 lRetVal = 0 那么 DownloadFile = True 结束函数

【讨论】:

  • 如果你edit你的答案并应用一点[格式化],它会大大改善它。
【解决方案2】:

这是我想出的。

 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

【讨论】:

    【解决方案3】:

    这是我知道的最简单的方法。

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

    这优于使用 Microsoft 的 documentation 中的 My.Computer.Network.DownloadFile 方法

    “DownloadFile 方法不发送可选的 HTTP 标头。如果可选的用户代理标头丢失,某些服务器可能会返回 500(内部服务器错误)。要发送可选标头,您必须使用 WebClient 类构造一个请求。”

    【讨论】:

    • 看起来很简单 =) 它会等待图像下载并执行下一个代码,还是我只会发送请求并执行下一个代码?
    • 是同步的,所以会等到文件下载完再继续。
    • 您应该使用“使用”。您的代码也是如此,但它更“安全”,因为它直接向您表明您没有忘记任何东西。
    【解决方案4】:

    有一个更简单的方法:

    My.Computer.Network.DownloadFile(Source, Desination)
    

    【讨论】:

    • 我喜欢这多么简单!
    • @WilliamContestabile 有理由使用我的解决方案而不是这个解决方案。我已经更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2010-10-17
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-12
    相关资源
    最近更新 更多