Emotion API C# Quick Start

This article provides information and a code sample to help you quickly get started using the Emotion API Recognize method with C# to recognize the emotions expressed by one or more people in an image.


  • Get the Microsoft Cognitive Emotion API Windows SDK here
  • Get your free Subscription Key here

Emotion Recognition C# Example Request

Create a new Console solution in Visual Studio, then replace Program.cs with the following code. Change the string uri to use the region where you obtained your subscription keys, and replace the "Ocp-Apim-Subscription-Key" value with your valid subscription key.

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

namespace CSHttpClientSample
    static class Program
        static void Main()
            Console.Write("Enter the path to a JPEG image file:");
            string imageFilePath = Console.ReadLine();


            Console.WriteLine("\n\n\nWait for the result below, then hit ENTER to exit...\n\n\n");

        static byte[] GetImageAsByteArray(string imageFilePath)
            FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
            BinaryReader binaryReader = new BinaryReader(fileStream);
            return binaryReader.ReadBytes((int)fileStream.Length);

        static async void MakeRequest(string imageFilePath)
            var client = new HttpClient();

            // Request headers - replace this example key with your valid key.
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "13hc77781f7e4b19b5fcdd72a8df7156");

            // 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 westcentralus, replace "westus" in the 
            //   URI below with "westcentralus".
            string uri = "https://westus.api.cognitive.microsoft.com/emotion/v1.0/recognize?";
            HttpResponseMessage response;
            string responseContent;

            // Request body. Try this sample with a locally stored JPEG image.
            byte[] byteData = GetImageAsByteArray(imageFilePath);

            using (var content = new ByteArrayContent(byteData))
                // This example uses content type "application/octet-stream".
                // The other content types you can use are "application/json" and "multipart/form-data".
                content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response = await client.PostAsync(uri, content);
                responseContent = response.Content.ReadAsStringAsync().Result;

            //A peak at the JSON response.

Recognize Emotions Sample Response

A successful call returns an array of face entries and their associated emotion scores, ranked by face rectangle size in descending order. An empty response indicates that no faces were detected. An emotion entry contains the following fields:

  • faceRectangle - Rectangle location of face in the image.
  • scores - Emotion scores for each face in the image.
    "faceRectangle": {
      "left": 68,
      "top": 97,
      "width": 64,
      "height": 97
    "scores": {
      "anger": 0.00300731952,
      "contempt": 5.14648448E-08,
      "disgust": 9.180124E-06,
      "fear": 0.0001912825,
      "happiness": 0.9875571,
      "neutral": 0.0009861537,
      "sadness": 1.889955E-05,
      "surprise": 0.008229999