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

时间:2015-12-07 12:48:57

标签: c# json image download wikipedia

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

            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中我用函数调用这个类,并手动输入所有图像,名称和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; 

我知道应该有更短的方法来简化编码。我尝试了很多。但是失败了。如果有人回答我应该做些什么来使它更通用或更具动态

,那就太好了

1 个答案:

答案 0 :(得分:1)

您的问题是减少服务电话的数量。根据维基百科[沙箱文档] [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个单独的方法