Emotion API In C# Using Universal Windows Platform

Before reading this article, go through the articles given below to get some knowledge about Cognitive services and Universal Windows Platform.

The following are the list of APIs available for Cognitive Services.

  • Vision API
  • Search API
  • Language API
  • Speech API
  • Knowledge API
Some requirements are given below, which are required to complete this article.
  • Visual Studio 2015 (Any Edition - Community Edition, Enterprise Edition) Community edition is a free software available on the internet.
  • Cognitive Service Emotion API Subscription Key. It’s a free account.
The steps are given below, which are required to complete this article.

Emotion API Key from Cognitive Service

Login to Cognitive Service by clicking the here.

Now, Cognitive subscription page appears, where you need to select “Emotion Preview” and accept Microsoft legal terms. Click activate button to activate your subscription key.

Subsequently, you will land on the following page. From this page, you will be able to find the emotion preview key. Click copy button to copy Emotion API key.

From this free account, you will be able to get two subscription keys for each product.

Working with Visual Studio

Open Visual Studio. Subsequently, click File and then click open. Finally Project/Solution is required to open your project files.

Browse project files from the desired location and click Open button to open the project, as shown below. Finally, your solution opens in Visual Studio.

Right-click on the project file and select Manage NuGet Package. This will open up NuGet Package Manager.


Now, in NuGet Package Manager search for “Newtonsoft.json” and select Newtonsoft.json package from dropdown. Select install and It takes about 10 to 20 seconds to successfully install Newtonsoft.json package.



  1. window.Log("EmotionServiceClient is created");  
  3.             //  
  4.             // Create Project Oxford Emotion API Service client  
  5.             //  
  6.             EmotionServiceClient emotionServiceClient = new EmotionServiceClient(subscriptionKey);  
  8.             window.Log("Calling EmotionServiceClient.RecognizeAsync()...");  
  9.             try  
  10.             {  
  11.                 //  
  12.                 // Detect the emotions in the URL  
  13.                 //  
  14.                 Emotion[] emotionResult = await emotionServiceClient.RecognizeAsync(url);  
  15.                 return emotionResult;  
  16.             }  
  17.             catch (Exception exception)  
  18.             {  
  19.                 window.Log("Detection failed. Please make sure that you have the right subscription key and proper URL to detect.");  
  20.                 window.Log(exception.ToString());  
  21.                 return null;  
  22.             }  


  1. // Create Project Oxford Emotion API Service client  
  2. //  
  3. EmotionServiceClient emotionServiceClient = new EmotionServiceClient(subscriptionKey);  
  5. window.Log("Calling EmotionServiceClient.RecognizeAsync()...");  
  6. try  
  7. {  
  8.     Emotion[] emotionResult;  
  9.     using (Stream imageFileStream = File.OpenRead(imageFilePath))  
  10.     {  
  11.         //  
  12.         // Detect the emotions in the URL  
  13.         //  
  14.         emotionResult = await emotionServiceClient.RecognizeAsync(imageFileStream);  
  15.         return emotionResult;  
  16.     }  
  17. }  
  18. catch (Exception exception)  
  19. {  
  20.     window.Log(exception.ToString());  
  21.     return null;  
  22. }  

Once installation completes, right click on the project and click build solution to build your solution (CTRL+SHIFT+B).

Click F5 button to deploy your solution.

Congrats. Now, your application is deploying.

Subsequently, paste your Emotion API subscription key in the text box and click Save button.


Delete key is used for removing a key from the computer.

Now, Microsoft Project Window displays. Select “Select Scenario” followed by clicking Detect emotion, using stream or by URL.

Now, upload an image and click accept project oxford license term to detect emotion, as shown below.


Download Emotion API class library from GitHub to download click here

In this article, we discussed Emotion API in C#, using Universal Windows Platform. I hope, you enjoyed reading this article.
Thanks for reading.
Happy coding. 

Up Next
    Ebook Download
    View all
    View all