title | titleSuffix | description | services | author | manager | ms.service | ms.subservice | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|
Bing Visual Search C# client library quickstart |
Bing Search Services |
Learn how to get image insights using the .NET client library for Bing Visual Search API. |
bing-search-services |
swhite-msft |
ehansen |
bing-search-services |
bing-visual-search |
quickstart |
07/15/2020 |
scottwhi |
Use this quickstart to begin getting image insights from the Bing Visual Search service, using the C# client library. While Bing Visual Search has a REST API compatible with most programming languages, the client library provides an easy way to integrate the service into your applications. The source code for this sample can be found on GitHub.
Reference documentation | Library source code | Package (NuGet) | Samples
- Visual Studio 2019.
- If you are using Linux/MacOS, this application can be run using Mono.
- The NuGet Visual Search package.
- From the Solution Explorer in Visual Studio, right-click on your project and select
Manage NuGet Packages
from the menu. Install theMicrosoft.Azure.CognitiveServices.Search.VisualSearch
package. Installing the NuGet packages also installs the following:- Microsoft.Rest.ClientRuntime
- Microsoft.Rest.ClientRuntime.Azure
- Newtonsoft.Json
- From the Solution Explorer in Visual Studio, right-click on your project and select
-
In Visual Studio, create a new project. Then add the following directives.
using Microsoft.Azure.CognitiveServices.Search.VisualSearch; using Microsoft.Azure.CognitiveServices.Search.VisualSearch.Models;
-
Instantiate the client with your subscription key.
var client = new VisualSearchClient(new ApiKeyServiceClientCredentials("YOUR-ACCESS-KEY"));
-
Create a
FileStream
to your images (in this caseTestImages/image.jpg
). Then use the client to send a search request usingclient.Images.VisualSearchMethodAsync()
.System.IO.FileStream stream = new FileStream(Path.Combine("TestImages", "image.jpg"), FileMode.Open); // The knowledgeRequest parameter is not required if an image binary is passed in the request body var visualSearchResults = client.Images.VisualSearchMethodAsync(image: stream, knowledgeRequest: (string)null).Result;
-
Parse the results of the previous query:
// Visual Search results if (visualSearchResults.Image?.ImageInsightsToken != null) { Console.WriteLine($"Uploaded image insights token: {visualSearchResults.Image.ImageInsightsToken}"); } else { Console.WriteLine("Couldn't find image insights token!"); } // List of tags if (visualSearchResults.Tags.Count > 0) { var firstTagResult = visualSearchResults.Tags[0]; Console.WriteLine($"Visual search tag count: {visualSearchResults.Tags.Count}"); // List of actions in first tag if (firstTagResult.Actions.Count > 0) { var firstActionResult = firstTagResult.Actions[0]; Console.WriteLine($"First tag action count: {firstTagResult.Actions.Count}"); Console.WriteLine($"First tag action type: {firstActionResult.ActionType}"); } else { Console.WriteLine("Couldn't find tag actions!"); } }
[!div class="nextstepaction"] Build a single-page web app.