How do I programmatically save an image from a URL?

C#.NetImage

C# Problem Overview


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 :)

C# Solutions


Solution 1 - C#

It would be easier to write something like this:

WebClient webClient = new WebClient();
webClient.DownloadFile(remoteFileUrl, localFileName);

Solution 2 - C#

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();
            }
        }
    }
}

Solution 3 - C#

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

Solution 4 - C#

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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser132291View Question on Stackoverflow
Solution 1 - C#OleksView Answer on Stackoverflow
Solution 2 - C#William EdmondsonView Answer on Stackoverflow
Solution 3 - C#opsgreatView Answer on Stackoverflow
Solution 4 - C#Paolo SoftlogicaView Answer on Stackoverflow