Search code examples
c#downloadsavestore

download and save image using c#


I generated three picture from wikipedia api.Now I want to store it in my current directory. with the following code I can successfully create folder with name.But it saves only one image, the last one. I am trying a lot. But could not fix how to save three images accordingly

      public static void Load_Image1(string name1, string name2, string name3,string LocationName)
    {
        var startPath = Application.StartupPath;
        string Imagefolder = Path.Combine(startPath, "Image");
        string subImageFolder = Path.Combine(Imagefolder, LocationName);
        System.IO.Directory.CreateDirectory(subImageFolder);
        //string Jpeg = Path.Combine(Environment.CurrentDirectory, subImageFolder);

        List<PictureBox> pictureBoxes = new List<PictureBox>();
        pictureBoxes.Add(Image1);
        pictureBoxes.Add(Image2);
        pictureBoxes.Add(Image3);

        using (var wc = new System.Net.WebClient())
        {
            var uri = ("https://en.wikipedia.org/w/api.php?action=query&prop=imageinfo&format=json&iiprop=url&iiurlwidth=400&titles="+name1+"|"+name2+"|"+name3);
            var response = wc.DownloadString(new Uri(uri));
            var responseJson = JsonConvert.DeserializeObject<RootObject>(response);

            List<string> urls = new List<string>();
            foreach (KeyValuePair<string, Pageval> entry in responseJson.query.pages)
            {
                var url = entry.Value.imageinfo.First().thumburl;
                urls.Add(url);
                var hash = uri.GetHashCode();
                string Jpeg = Path.Combine(Environment.CurrentDirectory, subImageFolder);
                var path = Path.Combine(Jpeg, hash.ToString("X") + ".jpg");
                wc.DownloadFile(url, path);

            }

            for (int i = 0; i < pictureBoxes.Count; i++)
            {
                Image1.SizeMode = PictureBoxSizeMode.StretchImage;
                Image2.SizeMode = PictureBoxSizeMode.StretchImage;
                Image3.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBoxes[i].Load(urls[i]);

                var hash = uri.GetHashCode();
                string Jpeg = Path.Combine(Environment.CurrentDirectory, subImageFolder);
                var path = Path.Combine(Jpeg, hash.ToString("X") + ".jpg");
                wc.DownloadFile(urls[i], path);

            }

        }
    }
}

Solution

  • You are downloading all images to the same filename on the disk - causing the first two images to be overwritten by the last one. The problem is that your base file name is based on

    var hash = uri.GetHashCode();
    

    This returns the same value since it's based on the url of all 3 images. Change to:

    var hash = url.GetHashCode();