How do I programmatically save an image from a URL? I am using C# and need to be able grab images from a URL and store them locally. ...and no, I am not stealing :)
4 Answers
It would be easier to write something like this:
WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFileUrl, localFileName);

- 31,955
- 11
- 77
- 132
-
4
-
Why `Dispose()`? http://stackoverflow.com/questions/6063079/will-an-object-be-disposed-automatically-after-an-asynchronous-event-it-subscrib – Alexander Jan 13 '14 at 11:03
-
1@Alexander, because it implements `IDisposable` interface, and hence should be disposed. But there is also `WebClient` (from Silverlight) that does not implement `IDisposable` interface, and can't be disposed in the way above. This answer is about `System.Net.WebClient` that may and should be disposed. – Oleks Jan 13 '14 at 13:52
-
I'd like to discuss this along with a drink. `IDisposable` must only be called if unmanaged resources are used, but all the other stuff is handled by the GC. Meaning, just because it implements the interface does not mean that it should be used. – Alexander Jan 13 '14 at 13:59
-
1@Alexander, come on! Calling `Dispose` ASAP means to free up the resources that are used by the object (handles/memory/whatever) ASAP too. Yes, for managed objects GC will save your bacon, but you cannot be sure when exactly. As for me, calling `Dispose` either explicily of using the `using` construction is a *rule of thumb*. – Oleks Jan 13 '14 at 14:50
-
1If you can give me the link to an article proving that, with measurable data, I will gladly learn and believe. I did a lot of tests a year ago and found that calling `Dispose()` was NOT freeing up memory at once. – Alexander Jan 13 '14 at 15:05
-
http://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238#538238 – Alexander Jan 13 '14 at 15:13
-
1@Alexander, yeah I've already seen this question and an excellent answer to it. Look onto the section about 250MB bitmap: *...But do you really want to leave 250MB of memory just sitting there – waiting for the garbage collector to eventually come along and free it? What if there's an open database connection? Surely we don't want that connection sitting open, waiting for the GC to finalize the object*. – Oleks Jan 13 '14 at 17:20
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45179/discussion-between-alexander-and-alex) – Alexander Jan 14 '14 at 10:18
-
@Alexander: just found one more example why disposables should be disposed http://stackoverflow.com/a/5912988/102112 – Oleks Apr 28 '16 at 13:17
-
Problem solved: using(var webClient = new WebClient()) { webClient.DownloadFile(remoteFileUrl, localFileName); } – Joseph White Jul 28 '17 at 02:47
You just need to make a basic http request using HttpWebRequest for the URI of the image then grab the resulting byte stream then save that stream to a file.
Here is an example on how to do this...
'As a side note if the image is very large you may want to break up br.ReadBytes(500000) into a loop and grab n bytes at a time writing each batch of bytes as you retrieve 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();
}
}
}
}

- 3,619
- 3
- 32
- 41
-
You might as well loop until the end of the stream instead of br.ReadBytes(500000); – nos Jul 10 '09 at 15:35
-
1This one is especially good for me - I don't need the physical file, just the stream, so it's much better than the accepted answer, in my case. – MGOwen Nov 05 '09 at 04:31
-
can you tell me that why @ is used before the location variable value setting. Indeed this answer is from 11 years ago – Ahmed Ali Jan 11 '20 at 12:44
-
1@AhmedsaysReinstateMonica - it prevents you from having to escape certain characters in the string, like the backslash. – ianpoley Jan 31 '20 at 17:20
An example in aspx (c#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
public partial class download_file_from_url : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string url = "http://4rapiddev.com/wp-includes/images/logo.jpg";
string file_name = Server.MapPath(".") + "\\logo.jpg";
save_file_from_url(file_name, url);
Response.Write("The file has been saved at: " + file_name);
}
public void save_file_from_url(string file_name, string url)
{
byte[] content;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();
using (BinaryReader br = new BinaryReader(stream))
{
content = br.ReadBytes(500000);
br.Close();
}
response.Close();
FileStream fs = new FileStream(file_name, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
try
{
bw.Write(content);
}
finally
{
fs.Close();
bw.Close();
}
}
}
Author: HOAN HUYNH
ASP.Net C# Download Or Save Image File From URL
-
I copied from [http://4rapiddev.com/csharp/asp-net-c-download-or-save-image-file-from-url/](http://4rapiddev.com/csharp/asp-net-c-download-or-save-image-file-from-url/) – opsgreat Oct 31 '14 at 11:13
My solution is pre save the image to de disk and then usa as a normal saved image:
remoteFile = "http://xxx.yyy.com/image1.png"; localFile = "c:\myimage.png";
WebClient webClient = new WebClient(); webClient.DownloadFile(remoteFile, localFile);
-
1Welcome to Stack Overflow. Please note that this is basically the same answer as the accepted answer above, which was left over a decade ago. – Jeremy Caney Apr 18 '20 at 19:53