C# 使用c语言下载并保存图像#

C# 使用c语言下载并保存图像#,c#,download,save,store,C#,Download,Save,Store,我从维基百科api生成了三张图片。现在我想把它存储在我当前的目录中。使用以下代码,我可以成功创建名为的文件夹。但它只保存一个图像,即最后一个图像。我试了很多。但无法解决如何相应地保存三张图像 public static void Load_Image1(string name1, string name2, string name3,string LocationName) { var startPath = Application.StartupPath;

我从维基百科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);

            }

        }
    }
}
public static void Load_Image1(字符串名称1、字符串名称2、字符串名称3、字符串位置名称)
{
var startPath=Application.startPath;
字符串Imagefolder=Path.Combine(startPath,“Image”);
string subImageFolder=Path.Combine(Imagefolder,LocationName);
System.IO.Directory.CreateDirectory(子ImageFolder);
//字符串Jpeg=Path.Combine(Environment.CurrentDirectory,subImageFolder);
List pictureBoxes=新列表();
pictureBoxes.Add(图像1);
图片框。添加(图像2);
pictureBoxes.Add(图3);
使用(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(新Uri(Uri));
var responseJson=JsonConvert.DeserializeObject(响应);
列表URL=新列表();
foreach(responseJson.query.pages中的KeyValuePair条目)
{
var url=entry.Value.imageinfo.First().thumburl;
添加(url);
var hash=uri.GetHashCode();
字符串Jpeg=Path.Combine(Environment.CurrentDirectory,subImageFolder);
var path=path.Combine(Jpeg,hash.ToString(“X”)+“.jpg”);
下载文件(url,路径);
}
for(int i=0;i
您正在将所有图像下载到磁盘上的同一文件名-导致前两个图像被最后一个图像覆盖。 问题是您的基本文件名基于

var hash = uri.GetHashCode();
这将返回相同的值,因为它基于所有3个图像的url。 改为:

var hash = url.GetHashCode();

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

public static void Load_Image1(string name1、string name2、string name3、string LocationName)
 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]);



          }

       }
    }
 }
{ var startPath=Application.startPath; 字符串Imagefolder=Path.Combine(startPath,“Image”); string subImageFolder=Path.Combine(Imagefolder,LocationName); System.IO.Directory.CreateDirectory(子ImageFolder); //字符串Jpeg=Path.Combine(Environment.CurrentDirectory,subImageFolder); List pictureBoxes=新列表(); pictureBoxes.Add(图像1); 图片框。添加(图像2); pictureBoxes.Add(图3); 使用(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(新Uri(Uri)); var responseJson=JsonConvert.DeserializeObject(响应); 列表URL=新列表(); foreach(responseJson.query.pages中的KeyValuePair条目) { var url=entry.Value.imageinfo.First().thumburl; 添加(url); var hash=url.GetHashCode(); 字符串Jpeg=Path.Combine(Environment.CurrentDirectory,subImageFolder); var path=path.Combine(Jpeg,hash.ToString(“X”)+“.jpg”); 下载文件(url,路径); } for(int i=0;i
谢谢您的回答。我现在修好了