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 .NET Components, 2nd Edition
    Programming .NET Components, 2nd Edition
    More InfoPractical Database Programming with Visual Basic.NET
    Practical Database Programming with Visual Basic.NET
    More InfoVisual Basic .NET All-In-One Desk Reference For Dummies
    Visual Basic .NET All-In-One Desk Reference For Dummies
    More InfoMurach's ASP.NET 4.5 Web Programming with VB 2012 (Training & Reference)
    Murach's ASP.NET 4.5 Web Programming with VB 2012 (Training & Reference)
    More InfoVisual Basic .NET Programming
    Visual Basic .NET Programming
    More InfoDatabase Programming with Visual Basic .NET
    Database Programming with Visual Basic .NET