Download image from url

VB.NET
Copy to clipboard
fablecode
  1. ''' <summary>
  2. ''' Function to download Image from website
  3. ''' </summary>
  4. ''' <param name="_URL">URL address to download image</param>
  5. ''' <returns>Image</returns>
  6. Public Function DownloadImage(_URL As String) As Image
  7.     Dim _tmpImage As Image = Nothing
  8.  
  9.     Try
  10.         ' Open a connection
  11.         Dim _HttpWebRequest As System.Net.HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(_URL), System.Net.HttpWebRequest)
  12.  
  13.         _HttpWebRequest.AllowWriteStreamBuffering = True
  14.  
  15.         ' You can also specify additional header values like the user agent or the referer: (Optional)
  16.         _HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
  17.         _HttpWebRequest.Referer = "http://www.google.com/"
  18.  
  19.         ' set timeout for 20 seconds (Optional)
  20.         _HttpWebRequest.Timeout = 20000
  21.  
  22.         ' Request response:
  23.         Dim _WebResponse As System.Net.WebResponse = _HttpWebRequest.GetResponse()
  24.  
  25.         ' Open data stream:
  26.         Dim _WebStream As System.IO.Stream = _WebResponse.GetResponseStream()
  27.  
  28.         ' convert webstream to image
  29.         _tmpImage = Image.FromStream(_WebStream)
  30.  
  31.         ' Cleanup
  32.         _WebResponse.Close()
  33.         _WebResponse.Close()
  34.     Catch _Exception As Exception
  35.         ' Error
  36.         Console.WriteLine("Exception caught in process: {0}", _Exception.ToString())
  37.         Return Nothing
  38.     End Try
  39.  
  40.     Return _tmpImage
  41. End Function
  42.  
  43.  

    Leave a comment

     
    Visual verification
     

    Related books

    More InfoLearning Visual Basic .Net
    Learning Visual Basic .Net
    More InfoBeginning Visual Basic 2015
    Beginning Visual Basic 2015
    More InfoProgramming Microsoft® Visual Basic® .NET (Core Reference) (Developer Reference)
    Programming Microsoft® Visual Basic® .NET (Core Reference) (Developer Reference)
    More InfoPractical Database Programming with Visual Basic.NET
    Practical Database Programming with Visual Basic.NET
    More InfoVisual Basic 2015 Unleashed
    Visual Basic 2015 Unleashed
    More InfoMurach's Visual Basic 2015
    Murach's Visual Basic 2015
    More InfoProgramming Microsoft® Visual Basic® .NET for Microsoft Access Databases (Developer Reference)
    Programming Microsoft® Visual Basic® .NET for Microsoft Access Databases (Developer Reference)
    More InfoMicrosoft Visual Basic .NET Programming for the Absolute Beginner
    Microsoft Visual Basic .NET Programming for the Absolute Beginner