Create the Visual Studio project

In Visual Studio, create a new Console app (.NET Framework) project and name it FaceDetection.

If there are other projects in your solution, select this one as the single startup project.

Add face detection code

Open the new project's Program.cs file. Here, you will add the code needed to load images and detect faces.

Include namespaces

Add the following using statements to the top of your Program.cs file.

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;

Add essential fields

Add the following fields to the Program class. This data specifies how to connect to the Face service and where to get the input data. You'll need to update the subscriptionKey field with the value of your subscription key, and you may need to change the uriBase string so that it contains the correct region identifier (see the Face API docs for a list of all region endpoints).

// Replace <Subscription Key> with your valid subscription key.
const string subscriptionKey = "<Subscription Key>";
// NOTE: You must use the same region in your REST call as you used to
// obtain your subscription keys. For example, if you obtained your
// subscription keys from westus, replace "westcentralus" in the URL
// below with "westus".
//
// Free trial subscription keys are generated in the "westus" region.
// If you use a free trial subscription key, you shouldn't need to change
// this region.
const string uriBase =
"https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect";

Receive image input

Add the following code to the Main method of the Program class. This writes a prompt to the console asking the user to enter an image URL. Then it calls another method, MakeAnalysisRequest, to process the image at that location.

Call the face detection REST API

Add the following method to the Program class. It constructs a REST call to the Face API to detect face information in the remote image (the requestParameters string specifies which face attributes to retrieve). Then it writes the output data to a JSON string.

Next steps

In this quickstart, you created a simple .NET console application that uses REST calls with the Azure Face API to detect faces in an image and return their attributes. Next, explore the Face API reference documentation to learn more about the supported scenarios.