You just need to make a basic HTTP request using the HttpWebRequest for the image URI, then take the received byte stream, then save this stream to a file.
Here is an example of how to do this ...
'As a side note, if the image is very large, you can break br.ReadBytes (500000) into a loop and capture n bytes at a time, writing down each batch of bytes as you extract them. ''
using System; using System.IO; using System.Net; using System.Text; namespace ImageDownloader { class Program { static void Main(string[] args) { string imageUrl = @"http://www.somedomain.com/image.jpg"; string saveLocation = @"C:\someImage.jpg"; byte[] imageBytes; HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl); WebResponse imageResponse = imageRequest.GetResponse(); Stream responseStream = imageResponse.GetResponseStream(); using (BinaryReader br = new BinaryReader(responseStream )) { imageBytes = br.ReadBytes(500000); br.Close(); } responseStream.Close(); imageResponse.Close(); FileStream fs = new FileStream(saveLocation, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); try { bw.Write(imageBytes); } finally { fs.Close(); bw.Close(); } } } }
William Edmondson Jul 10 '09 at 15:31 2009-07-10 15:31
source share