/// <summary> /// Function to download Image from Url
/// </summary>
/// URL address to download image
/// <returns>Image</returns>
private Image DownloadImage(string _URL)
{
Image _tmpImage = null;
Application.DoEvents();
try
{
// Open a connection
System.Net.HttpWebRequest _HttpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(_URL);
_HttpWebRequest.AllowWriteStreamBuffering = true;
// You can also specify additional header values like the user agent or the referer: (Optional)
_HttpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6b) Gecko/20031212 Firebird/0.7+";
_HttpWebRequest.Referer = "http://www.google.com/";
// set timeout for 20 seconds (Optional)
_HttpWebRequest.Timeout = 20000;
// Request response:
System.Net.WebResponse _WebResponse = _HttpWebRequest.GetResponse();
// Open data stream:
System.IO.Stream _WebStream = _WebResponse.GetResponseStream();
// convert webstream to image
_tmpImage = Image.FromStream(_WebStream);
// Cleanup
_WebResponse.Close();
_WebResponse.Close();
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
return null;
}
return _tmpImage;
}
Monday, May 29, 2017
How to Download image from Url For Web Scraping
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment