Bing图像搜索api过滤器大小

时间:2014-07-17 10:43:52

标签: c# .net bing bing-api

我正在使用bing图像搜索API从bing中检索一些图像。我想要检索中小型的小图像,具体取决于我想要实现的任务。但是,看起来我做错了。

这是我的代码:

            // This is the query - or you could get it from args.

            string query = keyword;

            // Create a Bing container.

            string rootUri = "https://api.datamarket.azure.com/Bing/Search";

            var bingContainer = new Bing.BingSearchContainer(new Uri(rootUri));

            // Configure bingContainer to use your credentials.

            bingContainer.Credentials = new NetworkCredential(uid, uid);

            // Build the query.


            var imageQuery = bingContainer.Image(keyword, null, null, null, null, null, null);

            switch (i)
            {
                case 0:
                    imageQuery = imageQuery.AddQueryOption("Size", "Small");
                    break;
                case 1:
                    imageQuery = imageQuery.AddQueryOption("Size", "Medium");
                    break;
                case 2:
                    imageQuery = imageQuery.AddQueryOption("Size", "Large");
                    break;
            }
            var imageResults = imageQuery.Execute();

            foreach (var result in imageResults)
            {

                Console.WriteLine(result.Title + ":" + result.MediaUrl);

            }

我在var imageResults = imageQuery.Execute()

收到错误消息

这是错误An error occurred while processing this request

imageQuery看起来像这样(抛出异常时):Link

它没有过滤器。

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以使用构造函数:

var imageQuery = bingContainer.Image(keyword, null, null, null, null, null, "Size:Small");

请注意,它在内部的作用是:

query = query.AddQueryOption("ImageFilters", string.Concat("\'", System.Uri.EscapeDataString(ImageFilters), "\'"));

query是你的imageQuery

因此,如果您不想使用构造函数,您应该能够按如下方式切换案例(0,1,2):

var imageQuery = bingContainer.Image(keyword, null, null, null, null, null, null);

string imageFilters;
switch (i)
{
    case 0:
        imageFilters= ("Size:Small");
        break;
    case 1:
        imageFilters= ("Size:Medium");
        break;
    case 2:
        imageFilters= ("Size:Large");
        break;
}
imageQuery = query.AddQueryOption("ImageFilters", string.Concat("\'", System.Uri.EscapeDataString(imageFilters), "\'"));