Skip to content

Instantly share code, notes, and snippets.

@Dynyx
Created June 4, 2012 13:36
Show Gist options
  • Save Dynyx/2868419 to your computer and use it in GitHub Desktop.
Save Dynyx/2868419 to your computer and use it in GitHub Desktop.
Download image from URL
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