我有一个Wikipedia API,通过它我可以从维基百科页面中提取三张图片。我想在三个PictureBoxes中显示这3个图像。但我真的不知道如何使用for循环在三个不同的PictureBox中显示三个图像。这是我的代码
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++)
{
}
}
}
}
}
答案 0 :(得分:1)
我猜你要将图片从var url
加载到PictureBox。如果是这种情况,您可以创建一个数组或列表的PictureBoxes,然后循环该列表(或数组)。所以:
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]);
}
}
}
}