C#: Programmatically download all Images from a website and save them locally.

For this example, I will be downloading all the .gifs from a specific page on imgur. Then, I will save them into a folder called _Images.

This is the url to the specific page I will be downloading images from http://imgur.com/a/GPlx4

What the website looks like:

Below is C# code that will:
-use a WebClient.DownloadString() to download the html into a string
-parse out specific image links from the returned html string using regular expressions
-use another WebClient.DownloadFile() to download each image link parsed

WebClient wchtml = new WebClient();
string htmlString = wchtml.DownloadString("http://imgur.com/a/GPlx4");
int mastercount = 0;
Regex regPattern = new Regex(@"http://i.imgur.com/(.*?)alt=""", RegexOptions.Singleline);
MatchCollection matchImageLinks = regPattern.Matches(htmlString );
 
foreach (Match img_match in matchImageLinks)
{
    string imgurl = img_match.Groups[1].Value.ToString();
    Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?",
    RegexOptions.IgnoreCase);
    MatchCollection ms = regx.Matches(imgurl);
    foreach (Match m in ms)
    {
        Console.WriteLine("Downloading..  "  + m.Value);
        mastercount++;
        try
        {
            WebClient wc = new WebClient();
            wc.DownloadFile(m.Value, @"C:\_Images\bg_" + mastercount + ".gif");
            Thread.Sleep(1000);
        }
        catch (Exception x)
        {
            Console.WriteLine("Failed to download image.");
        }
        break;
    }
}

Output: C:\_Images

6 thoughts on “C#: Programmatically download all Images from a website and save them locally.

  1. Ali

    Dear Sir,

    What will be the regex pattern for downloading all images from any website.
    and
    What will be the regex pattern for downloading all image files from any website.

    Reply
  2. farah

    dear sear,
    I execute yous code but it returned one image in the folder _Images.
    I can’t understand the reason.
    Please give me the response.
    thank you

    Reply
    1. ajay

      I also execute yours code but it returned one image in the folder _Images.
      I can’t understand the reason.
      Please give me the response.
      thank you

      Reply
  3. Jahangeer

    Hi Sir,

    I am doing the same thing (In c# ) that is downloading Images from URL but the image size is not original one. For Example there is one image 1.jpeg that is uploaded by someone and size of this is 2000 bytes. Now if I am downloading the same file that is 1.jpeg Programmatically then sometime downloaded file’s size becomes 2001 bytes and sometimes 1990 bytes.

    Uri url = new Uri(URL of the image that need to download);
    System.Net.HttpWebRequest webRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);

    webRequest.AllowWriteStreamBuffering = true;
    webRequest.Timeout = 30000;
    System.Net.WebResponse webResponse = webRequest.GetResponse();

    System.IO.Stream stream = webResponse.GetResponseStream();
    Image image = System.Drawing.Image.FromStream(stream);

    webResponse.Close();
    webResponse.Dispose();

    String rootPath = @”C:\download_my”;
    String fileName = System.IO.Path.Combine(rootPath, imageCounter+”.jpeg”);
    filename1 = “C:\\download_my\\test1.jpeg”;
    image.Save(fileName);

    Can you tell me that why this is happening and solution for It. So I get same size of image after downloading it.

    Thank You

    Reply
  4. Arsman Ahmad

    There’s a huge bug in this code in TRY-Block. The issue is in
    ___________________________________________________________________________________
    1. WebClient wc = new WebClient();
    2. wc.DownloadFile(m.Value, @”C:\_Images\bg_” + mastercount + “.gif”); // Issue Line
    3. Thread.Sleep(1000); // Issue Line
    ___________________________________________________________________________________
    Because, when we execute the code, it run as much fast so that if 1st image is being downloading, then 2nd line of above code is not capable to execute. Infact, 3rd line of above code is also useless here, because every image couldn’t be finished its downloading in this SLEEP-Time. So, the best way to download a bulk of images is ASYNC call.
    We can call all the URLs to download the required images in ASYNC mode, and it’ll download all the images in few minutes after the execution of your code(Depends on your internet speed)
    _________________________________________________________________________________________________
    1. WebClient wc = new WebClient();
    2. wc.DownloadFileAsync(m.Value, @”C:\_Images\bg_” + mastercount + “.gif”); // Solution
    _________________________________________________________________________________________________

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *