Created
June 4, 2012 13:36
-
-
Save Dynyx/2868419 to your computer and use it in GitHub Desktop.
Download image from URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Image DownloadImage(string url) | |
{ | |
Image image = null; | |
try | |
{ | |
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url); | |
httpWebRequest.AllowWriteStreamBuffering = true; | |
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201"; | |
httpWebRequest.Referer = "http://www.google.com/"; | |
httpWebRequest.Timeout = 20000; | |
var webResponse = httpWebRequest.GetResponse(); | |
var webStream = webResponse.GetResponseStream(); | |
if (webStream != null) image = Image.FromStream(webStream); | |
webResponse.Close(); | |
webResponse.Close(); | |
} | |
catch (Exception) | |
{ | |
return null; | |
} | |
return image; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment