Menu

Want to work with us? We're on the lookout for a UX/UI Designer and Digital Project Manager.

We're hiring

Download and Save Image Using URL

Web Bureau


04 April 2011 by Web Bureau

If you need to download images across sites try using this function.

Public Function getImageByUrl(ByVal url As String, ByVal filename As String) As Boolean

        Dim response As WebResponse = Nothing
        Dim remoteStream As Stream = Nothing
        Dim readStream As StreamReader = Nothing
        Try
            Dim request As WebRequest = WebRequest.Create(url)
            If request IsNot Nothing Then
                response = request.GetResponse()
                If response IsNot Nothing Then
                    remoteStream = response.GetResponseStream()

                    readStream = New StreamReader(remoteStream)

                    Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(remoteStream)

                    If img Is Nothing Then
                        Return False
                    End If

                    img.Save(System.Web.HttpContext.Current.Server.MapPath("\databaseimages\") & filename, System.Drawing.Imaging.ImageFormat.Jpeg)
                    img.Dispose()
                End If
            End If
        Finally
            If response IsNot Nothing Then
                response.Close()
            End If
            If remoteStream IsNot Nothing Then
                remoteStream.Close()
            End If
            If readStream IsNot Nothing Then
                readStream.Close()
            End If
        End Try

        Return True
    End Function

Grow your businessStart a project with us today.