2015-12-12 117 views
1

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

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

      } 

     } 
    } 
} 

回答

1

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

var hash = uri.GetHashCode(); 

,因为它是基于所有3张图片的URL这将返回相同的值。 更改为:

var hash = url.GetHashCode(); 
+0

谢谢您的回答。这是工作。@ EyIM – user3193794

0

你居然保存所有照片,但具有相同的名称,这就是为什么只有最后保留在文件系统(你保持覆盖图像)。您应该在可变路径中使用唯一标识符,以便您区分图像,并以不同名称保存以避免覆盖

+0

谢谢你的回答。我现在修好了 – user3193794

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



      } 

     } 
    } 
}