user3193794
user3193794

Reputation: 105

Load three images in three different picture box using c#

I have a Wikipedia API through which I can extract three images from Wikipedia page. I want to show these 3 images in three PictureBoxes. But I really do not know how to make a for loop to show three images in three different PictureBoxes. Here is my code

class Image
{
    public static PictureBox Image1 = new PictureBox();
    public static PictureBox Image2 = new PictureBox();
    public static PictureBox Image3 = new PictureBox();


    public static void Load_Image1()
    {
        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=File%3ALuftbild%20Flensburg%20Schleswig-Holstein%20Zentrum%20Stadthafen%20Foto%202012%20Wolfgang%20Pehlemann%20Steinberg-Ostsee%20IMG%206187.jpg%7CFile%3AHafen%20St%20Marien%20Flensburg2007.jpg%7CFile%3ANordertor%20im%20Schnee%20(Flensburg%2C%20Januar%202014).JPG");
            var response = wc.DownloadString(new Uri(uri));
            var responseJson = JsonConvert.DeserializeObject<RootObject>(response);

            foreach (KeyValuePair<string, Pageval> entry in responseJson.query.pages)
            {
                var url = entry.Value.imageinfo.First().thumburl;
                for(int i; i<=3 ;i++)
                {

                }

            }

        }
    }
}

Upvotes: 0

Views: 192

Answers (1)

Misa Lazovic
Misa Lazovic

Reputation: 2823

I'm guessing you want to load picture from var url to PictureBox. If that's the case, you can make an array or list of PictureBoxes and than loop that list (or array). So:

class Image
{
    public static PictureBox Image1 = new PictureBox();
    public static PictureBox Image2 = new PictureBox();
    public static PictureBox Image3 = new PictureBox();

    public static void Load_Image1()
    {
        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=File%3ALuftbild%20Flensburg%20Schleswig-Holstein%20Zentrum%20Stadthafen%20Foto%202012%20Wolfgang%20Pehlemann%20Steinberg-Ostsee%20IMG%206187.jpg%7CFile%3AHafen%20St%20Marien%20Flensburg2007.jpg%7CFile%3ANordertor%20im%20Schnee%20(Flensburg%2C%20Januar%202014).JPG");
            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);                        
            }
            // if you're sure that you're getting always at least 3 images, this will work
            for(int i = 0; i < pictureBoxes.Count; i++)
            {
                pictureBoxes[i].Load(urls[i]);
            }
        }
    }
}

Upvotes: 1

Related Questions