1

我有一些用于从 VB.NET 中的 URL 保存图像的代码,但我没有找到保存图像的位置,我添加了一个位置字符串,但我不知道如何在代码中使用它。如何更改我的代码保存图像的位置(导入 System.Drawing 公共类 Form1)

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim MyImage As System.Drawing.Image
    Dim URL As String = TextBox1.Text
    Dim FileName As String, URLpieces As String()
    Dim location As String = "C:/SaveIMGURL/"
    URLpieces = Split(URL, "/")
    FileName = URLpieces.GetValue(UBound(URLpieces))
    MyImage = GetImage(URL)
    MyImage.Save(FileName)
    MyImage = Nothing
End Sub
Function GetImage(ByVal URL As String) As System.Drawing.Image
    Dim Request As System.Net.HttpWebRequest
    Dim Response As System.Net.HttpWebResponse
    Request = System.Net.WebRequest.Create(URL)
    Response = CType(Request.GetResponse, System.Net.WebResponse)
    If Request.HaveResponse Then
        If Response.StatusCode = Net.HttpStatusCode.OK Then
            GetImage = System.Drawing.Image.FromStream(Response.GetResponseStream)
        End If
    End If
    Try
    Catch e As System.Net.WebException
        MsgBox("A web exception has occured [" & URL & "]." & vbCrLf & " System returned: " & e.Message, MsgBoxStyle.Exclamation, "Error!")
        Exit Try
    Catch e As System.Net.ProtocolViolationException
        MsgBox("A protocol violation has occured [" & URL & "]." & vbCrLf & "  System returned: " & e.Message, MsgBoxStyle.Exclamation, "Error!")
        Exit Try
    Catch e As System.Net.Sockets.SocketException
        MsgBox("Socket error [" & URL & "]." & vbCrLf & "  System returned: " & e.Message, MsgBoxStyle.Exclamation, "Error!")
        Exit Try
    Catch e As System.IO.EndOfStreamException
        MsgBox("An IO stream exception has occured. System returned: " & e.Message, MsgBoxStyle.Exclamation, "Error!")
        Exit Try
    Finally
    End Try
End Function

结束类

4

0 回答 0