2017-02-12 91 views
5

我在github上發現了這段代碼。當你鍵入someting時,它會搜索google並在picturebox1中顯示第一張圖片 - 現在我想添加3-4個其他圖片盒,並且我希望它能顯示其他圖片(如第二張和第三張 - 不僅僅是第一張) )。我的問題是我無法理解如何去做。在C#中的Google圖片搜索#

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 

我試過的東西:我刪除了休息;但它只是繼續搜索,它永遠不會停止。我希望它像其他網站(例如:在每頁顯示5-10圖片)。我應該改變什麼?我究竟做錯了什麼 ?

+1

嗨,這將是一件好事,提供更多有關您的問題的信息,例如GitHub上的代碼片段來自哪裏(即url)。 根據我的理解,您有一個文本框,用戶可以在其中輸入利用Google搜索API的某些內容(例如關鍵字),以獲取與您的關鍵字相關的一些圖片。你只能得到一張照片,並會獲得更多。我可以建議你閱讀CodeProject上的文章,這似乎是你在找什麼:https://www.codeproject.com/articles/11876/an-api-for-google-image-search – Ehouarn

+0

也似乎是Google圖片搜索API已棄用,因此您可以在此處查看自定義搜索:https://developers.google.com/custom-search/docs/overview 這包括指向RESTful API(JSON)文檔的鏈接 – Ehouarn

+0

謝謝你 - 我看到了這兩個鏈接 - 該項目(谷歌圖像搜索API)不工作了,我看到了第二個鏈接,但我仍然有問題:( – Johny

回答

2

除了刪除來自環路的破,取第一圖像5只

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

上述過濾器是由一個LINQ方法完成,當然也可以改變數。 我想你不想使用pictureBox1也爲其他圖像:你可以在foreach循環中創建一個帶有new的PictureBox控件,並將它們添加到Controls集合

+0

非常感謝你,我一直在尋找那個「 (5)「再次感謝你。 – Johny