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);
-
4
-
1@Alexander, because it implements
IDisposable
interface, and hence should be disposed. But there is alsoWebClient
(from Silverlight) that does not implementIDisposable
interface, and can't be disposed in the way above. This answer is aboutSystem.Net.WebClient
that may and should be disposed.– OleksJan 13, 2014 at 13:52 -
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, callingDispose
either explicily of using theusing
construction is a rule of thumb.– OleksJan 13, 2014 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. Jan 13, 2014 at 15:05 -
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.– OleksJan 13, 2014 at 17:20
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();
}
}
}
}
-
You might as well loop until the end of the stream instead of br.ReadBytes(500000);– nosJul 10, 2009 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.– MGOwenNov 5, 2009 at 4:31
-
can you tell me that why @ is used before the location variable value setting. Indeed this answer is from 11 years ago Jan 11, 2020 at 12:44
-
1@AhmedsaysReinstateMonica - it prevents you from having to escape certain characters in the string, like the backslash.– ianpoleyJan 31, 2020 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/– opsgreatOct 31, 2014 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. Apr 18, 2020 at 19:53