维基百科 api 用于图像从 json 下载到 c#

Wikipedia api for image download fron json to c#

我制作了一张图片 class,我用它来从维基百科下载图片 api。我制作了 3 个功能以在 3 个图片框中显示 3 个图像。我使用来自 json wiki api 的 "thumburl" 图片 link。这是我的图片 class 代码。

            class Images
            {

                public static PictureBox Image1 = new PictureBox();
                public static PictureBox Image2 = new PictureBox();
                public static PictureBox Image3 = new PictureBox();
                public static Label Image1_title = new Label();
                public static Label Image2_title = new Label();
                public static Label Image3_title = new Label();


                public static void Load_Image1(string name, string id,string LocationName)
                {


                    string Jpeg = Path.Combine(Environment.CurrentDirectory, @"C:\C# tutorial Backup\Tourist_Place\Images\"+LocationName);

                    using (WebClient wc = new WebClient())
                    {
                        var client = new WebClient();
                        var uri = ("https://en.wikipedia.org/w/api.php?action=query&prop=imageinfo&iiprop=comment|url|dimensions&format=json&iiurlwidth=400&titles=" + name);
                        var response = client.DownloadString(new Uri(uri));
                        JObject obj = JObject.Parse(response);

                        string image1 = (string)obj["query"]["pages"][id]["imageinfo"][0]["thumburl"];
                        Image1.SizeMode = PictureBoxSizeMode.StretchImage;
                        Image1.LoadAsync(image1);

                        string image1_Title = (string)obj["query"]["pages"][id]["title"];
                        Image1_title.Text = image1_Title;

                        var hash = uri.GetHashCode();
                        var path = Path.Combine(Jpeg, hash.ToString("X") + ".jpg");
                        client.DownloadFile(image1, path);


                    }

                }

                public static void Load_Image2(string name, string id,string LocationName)
                {

                   string Jpeg = Path.Combine(Environment.CurrentDirectory, @"C:\C# tutorial Backup\Tourist_Place\Images\" + LocationName);


                    using (WebClient wc = new WebClient())
                    {
                        var client = new WebClient();
                        var uri = ("https://en.wikipedia.org/w/api.php?action=query&prop=imageinfo&iiprop=comment|url|dimensions&format=json&iiurlwidth=400&titles=" + name);
                        var response = client.DownloadString(new Uri(uri));
                        JObject obj = JObject.Parse(response);


                        string image2 = (string)obj["query"]["pages"][id]["imageinfo"][0]["thumburl"];
                        Image2.SizeMode = PictureBoxSizeMode.StretchImage;
                        Image2.LoadAsync(image2);

                        string image2_Title = (string)obj["query"]["pages"][id]["title"];
                        Image2_title.Text = image2_Title;

                        var hash = uri.GetHashCode();
                        var path = Path.Combine(Jpeg, hash.ToString("X") + ".jpg");
                        client.DownloadFile(image2, path);
                    }

                }

                public static void Load_Image3(string name, string id, string LocationName)
                {

                    //string fileName = name + ".jpg";//plaese  change the storage location

                    string Jpeg = Path.Combine(Environment.CurrentDirectory, @"C:\C# tutorial Backup\Tourist_Place\Images\"+ LocationName);
                    using (WebClient wc = new WebClient())
                    {
                        var client = new WebClient();
                        var uri = ("https://en.wikipedia.org/w/api.php?action=query&prop=imageinfo&iiprop=comment|url|dimensions&format=json&iiurlwidth=400&titles=" + name);
                        var response = client.DownloadString(new Uri(uri));
                        JObject obj = JObject.Parse(response);


                        string image3 = (string)obj["query"]["pages"][id]["imageinfo"][0]["thumburl"];
                        Image3.SizeMode = PictureBoxSizeMode.StretchImage;
                        Image3.LoadAsync(image3);//

                        string image3_Title = (string)obj["query"]["pages"][id]["title"];
                        Image3_title.Text = image3_Title;

                        var hash = uri.GetHashCode();
                        var path = Path.Combine(Jpeg, hash.ToString("X") + ".jpg");
                        client.DownloadFile(image3, path);
                    }

                }



            }


        } 

现在在 form.cs 中,我用函数调用此 class 并手动输入所有图像、名称和 ID 的值。这是我的 form1.cs

      namespace Wikipedia_Image_Text_24_11_2015
     {
    public partial class Form1 : Form
    {

    public Form1()
    {
        InitializeComponent();

        POI_List.Combo_list = comboBox1;
        POI_List.List();


    }

     public void Download_Click(object sender, EventArgs e)
      {


        Images.Image1 = pictureBox1;
        Images.Image2 = pictureBox2;
        Images.Image3 = pictureBox3;

        Images.Image1_title = label1;
        Images.Image2_title = label2;
        Images.Image3_title = label3;




        switch (comboBox1.Text)
        {

           case "Heidelberg Castle":

                Images.Load_Image1("File:Heidelberg-Schlo%C3%9F.JPG", "-1", "Heidelberg Castle");
                Images.Load_Image2("File:Heidelberg%2020060420%20021.jpg", "-1", "Heidelberg Castle");
                Images.Load_Image3("File:Rondell%20Heidelberger%20Schloss%20vom%20Stueckgarten.jpg", "-1", "Heidelberg Castle");


                break;


            case "Neuschwanstein Castle":
               Images.Load_Image1("File:Schloss%20Neuschwanstein%202013.jpg", "40677196", "Neuschwanstein Castle");
                Images.Load_Image2("File:Neuschwanstein%20castle.jpg", "-1", "Neuschwanstein Castle");
                Images.Load_Image3("File:Hohenschwangau_-_Schloss_Neuschwanstein5.jpg", "-1", "Neuschwanstein Castle");




                break; 

我知道应该有更短的方法来使这个编码 easier.I 尝试了很多。但是 failed.It 如果有人回答我我应该怎么做才能使它更通用或更动态

会很好

您的问题是减少服务调用次数。根据维基百科 [沙盒文档][1]

[1]: https://en.wikipedia.org/wiki/Special:ApiSandbox#action=query&prop=imageinfo&format=json&titles=File%3ANeuschwanstein%20castle.jpg%7CFile%3AHohenschwangau_-_Schloss_Neuschwanstein5.jpg

它在请求中接受的不仅仅是文件(标题)。所以只需拨打一个 REST 电话即可。您将获得页面数组。解析它。遍历数组并显示图像。您不需要 3 个单独的方法