Google Image Search in C #

I found this code on github. when you type in, it will search on google and display the first u image in picturebox1 - now I want to add 3-4 more windows, and I want it to show other photos (both the second and the third), and not only first). my problem is that I cannot figure out how to do this.

try { this.Cursor = Cursors.WaitCursor; this._lblStatus.Text = "Searching..."; this._lblStatus.Update(); List<String> images_urls = t.SearchForImages (this._editImageText.Text.Trim()); if (t.Error == null && images_urls.Count > 0) { //Show first image only foreach (String image_url in images_urls) { Bitmap bitmap = ImageUtil.LoadPicture(image_url); if (bitmap != null) { //sometime the server refuses getting the image directly Image image = ImageUtil.ResizeImage(bitmap, pictureBox1, true); pictureBox1.Image = image; if (bitmap != null) bitmap.Dispose(); break; //show only one image 

What I tried: I deleted break ;, but it just keeps searching and never stops. I want this to look like other sites (ex: show 5-10 pic on each page). what should i change? What am I doing wrong?

+5
source share
1 answer

In addition to removing the break from the loop, take only the first 5 images

  foreach (String image_url in images_urls.Take(5)) { 

The above filter is performed by the Linq method, of course, you can change the number. I think you do not want to use pictureBox1 for other images: you can create PictureBox controls with new in the foreach loop and add them to the Controls collection

+2
source

Source: https://habr.com/ru/post/1264128/


All Articles