Recently I came across a situation where I need to download Images from a web site. Here is a tiny little code through which I have done that. I am sharing it for your reference.
////// Downloads the image from URL. /// /// "imageUrl">The image URL. /// "imagePath">The image path. public void DownloadImageFromUrl(string imageUrl,string imagePath) { try { System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(imageUrl); httpWebRequest.AllowWriteStreamBuffering = true; httpWebRequest.Timeout = 30000; using (System.Net.WebResponse webResponse = httpWebRequest.GetResponse()) { System.IO.Stream stream = webResponse.GetResponseStream(); System.Drawing.Image image = System.Drawing.Image.FromStream(stream); image.Save(imagePath); } } catch (Exception ex) { //Do some Exception Handling There } }
Here is how you can call above function
string downloadPath = "d:\\test.jpg";
DownloadImageFromUrl("http://test.com/test/test.ashx?ImageID=" + x + ".jpg", downloadPath);
DownloadImageFromUrl("http://test.com/test/test.ashx?ImageID=" + x + ".jpg", downloadPath);
Happy Coding !!!