【问题标题】:download and save image using c#使用c#下载并保存图像
【发布时间】:2016-03-18 15:45:48
【问题描述】:

我从 wikipedia api 生成了三张图片。现在我想将它存储在我的当前目录中。使用下面的代码,我可以成功创建带有名称的文件夹。但它只保存一张图像,最后一张。我正在尝试很多。但无法解决如何相应地保存三张图片

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

            }

        }
    }
}

【问题讨论】:

    标签: c# download save store


    【解决方案1】:

    您正在将所有图像下载到磁盘上的相同文件名 - 导致前两个图像被最后一个覆盖。 问题是您的基本文件名基于

    var hash = uri.GetHashCode();
    

    这将返回相同的值,因为它基于所有 3 个图像的 url。 改为:

    var hash = url.GetHashCode();
    

    【讨论】:

      【解决方案2】:

      您实际上保存了所有图片,但名称相同,这就是为什么文件系统中只保留最后一张(您不断覆盖图像)。您应该在变量路径中使用唯一标识符,以便区分图像,用不同的名称保存它们以避免覆盖

      【讨论】:

      • 感谢您的回答。我现在修好了
      【解决方案3】:
       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 = url.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]);
      
      
      
                }
      
             }
          }
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多