Bing 是微軟開發的搜尋引擎,使用者可透過 Bing API 開發程式,本文使用 Bing API 做圖片搜尋。
更多文章,請到我在點部落所建立的部落格「.NET菜鳥自救會」閱讀
http://www.dotblogs.com.tw/chou/
簡介
Bing 是微軟開發的搜尋引擎,使用者可透過 Bing API 開發程式,本文使用 Bing API 做圖片搜尋。
想要使用 Bing API,需申請 AppID,於網址 http://www.bing.com/developers/default.aspx,使用 Live Passport 登入,並輸入相關資訊,註冊後得到一組 AppID
方法
(1) 加入 Web 參考
在 [URL] 中輸入 http://api.search.live.net/search.wsdl?AppID=申請的AppID,修改 [Web 參考名稱],點選 [加入參考]
在程式中 using 專案名稱.參考名稱
using SmartDeviceBing.net.live.search.api;
(2) 程式碼
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SmartDeviceBing.net.live.search.api;
namespace SmartDeviceBing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSearch_Click(object sender, EventArgs e)
{
// LiveSearchService implements IDisposable.
using (LiveSearchService service = new LiveSearchService())
{
try
{
SearchRequest request = BuildRequestWeb();
// Send the request; display the response.
SearchResponse response = service.Search(request);
DisplayResponseWeb(response);
}
catch (System.Net.WebException ex)
{
MessageBox.Show(ex.Message);
}
}
}
private SearchRequest BuildRequestWeb()
{
SearchRequest request = new SearchRequest();
// Common request fields (required)
request.AppId = "B692A148D1624C4E3C1248C8E5DDC209E524D2C4";
request.Query = this.txtQuery.Text;
request.Sources = new SourceType[] { SourceType.Image };
// Common request fields (optional)
request.Version = "2.0";
request.Market = "en-us";
request.Adult = AdultOption.Moderate;
request.AdultSpecified = true;
request.Options = new SearchOption[]
{
SearchOption.EnableHighlighting
};
// Web-specific request fields (optional)
request.Web = new WebRequest();
request.Web.Count = 10;
request.Web.CountSpecified = true;
request.Web.Offset = 0;
request.Web.OffsetSpecified = true;
request.Web.Options = new WebSearchOption[]
{
WebSearchOption.DisableHostCollapsing,
WebSearchOption.DisableQueryAlterations
};
return request;
}
private void DisplayResponseWeb(SearchResponse response)
{
this.lstView.Items.Clear();
// Display the results header.
this.labBing.Text = string.Format("Displaying {0} to {1} of {2} results",
response.Image.Offset + 1,
response.Image.Offset + response.Image.Results.Count(),
response.Image.Total);
// Display the Image results.
foreach (ImageResult result in response.Image.Results)
{
ListViewItem lvItem = new ListViewItem(result.Title);
lvItem.SubItems.Add(result.MediaUrl);
lvItem.SubItems.Add(result.Width + "x" + result.Height);
this.lstView.Items.Add(lvItem);
}
}
private void lstView_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < lstView.Items.Count; i++ )
{
if (lstView.Items[i].Selected)
{
// 讀取網路圖片到 PictureBox
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(lstView.Items[i].SubItems[1].Text);
System.Net.HttpWebResponse rsp = (System.Net.HttpWebResponse)req.GetResponse();
if (rsp.StatusCode == System.Net.HttpStatusCode.OK)
{
this.PicBoxPhoto.Image = new Bitmap(rsp.GetResponseStream());
}
rsp.Close();
}
}
}
}
}
(3) 執行結果