I wrote a code by which I generated three images from wikipedia api.This is the code which I used for getting three images from api.
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(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); } // if you're sure that you're getting always at least 3 images, this will work 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]); } } } } } Now from Form1.cs I call this class by giving three image name in the method in this way-
public void Download_Click(object sender, EventArgs e) { switch (comboBox1.Text) { case "Flensburg": Images.Load_Image("File:Luftbild Flensburg Schleswig-Holstein Zentrum Stadthafen Foto 2012 Wolfgang Pehlemann Steinberg-Ostsee IMG 6187.jpg", "File:Hafen St Marien Flensburg2007.jpg", "File:Nordertor im Schnee (Flensburg, Januar 2014).JPG", comboBox1.Text); break; } } Now this coding is ok for 10 to 20 places. But if I want to get images for 1000 of places, then it is not a good practice. In this situation what should be the possible way to make this code dynamic.
No comments:
Post a Comment