
In this azure tutorial, we will discuss How To Generate Thumbnails Azure Cognitive Services. Apart from this, we will also discuss a few other topics Creating the Computer Vision API Azure Portal, Why Thumbnail Service?, create a console application using Visual Studio 2019 to generate a thumbnail, Generate thumbnails with Azure Vision API (NodeJS).
How To Generate Thumbnails Azure Cognitive Services? To generate thumbnails, you need to do the following implementation
- Creating the Computer Vision API service in Azure Portal.
- Develop a console application using Visual Studio 2019 write the C# logic and use the key value and the endpoint of the Azure Computer vision API to generate a thumbnail.
We will see the implementation in details below.
Table of Contents
- How To Generate Thumbnails Azure Cognitive Services
- Azure Cognitive Services
- What is Computer Vision API?
- Why Thumbnail Service?
- Prerequisites
- Create the Computer Vision API Azure Portal
- Setting up cognitive services account
- Create a console application using Visual Studio 2019 to generate a thumbnail
- Create thumbnails using Azure Cognitive Services
- Creating Thumbnails
- Generate thumbnails with Azure Vision API (NodeJS)
- Wrapping Up
How To Generate Thumbnails Azure Cognitive Services
Well, As part of this Article, we will discuss Generate a thumbnail – REST, C# – Azure Cognitive Services. Before starting the actual functionality, it is important that we should have a little bit of an idea of Azure Cognitive Services and Azure Computer Vision API. Then we should know the Prerequisites needed to start the actual development activity.
- How To Use Named Entity Recognition In Text Analytics
- Language Detection With Azure Cognitive Service
Azure Cognitive Services
This is one of the excellent services from Microsoft that helps the developers to implement Artificial Intelligence (AI), Machine Learning (ML) without having the expertise in these areas. The developer just needs to call an API to implement the ability to see, hear, speak, search, etc.
What is Computer Vision API?
Azure Computer Vision API is the part of Azure Cognitive Services, an Artificial Intelligence (AI) service API provided by Microsoft that helps the developers with an excellent advanced algorithm for processing the Images and can analyze the content of an image or a video in a very effective way and then it provides all the detailed information about that images.
Another important part is, you can easily create a thumbnail using the Azure Cognitive Services Computer Vision API and that provides you the best quality thumbnail. It has an endpoint to create thumbnails with a smart cropping feature. The API can able to identify the essence of the image, e.g. a face, and it crops around it.
Why Thumbnail Service?
Earlier, the GetThumbnailImage() method in the .NET framework was helping in creating the Thumbnail. But to be honest, there were a lot of issues with the quality and size of the image.
Azure Cognitive Services Computer vision Service API solves all the issues and generates the best thumbnails with the best quality and has the intelligent feature to decide which part of the image is best for the thumbnails.
Prerequisites
- You must have an Azure Account or a valid Azure Subscription. If you don’t have an Azure account till now. Create a Free Azure Account now.
- You must have Visual Studio 2019 installed on your local machine. If you don’t have Visual Studio 2019 installed on your machine as of now, then download and install Visual Studio 2019 on your system now.
As part of this development activity we will be performing the below activities
- Creating the Computer Vision API service in Azure Portal.
- Create a console application using Visual Studio 2019 to generate a thumbnail.
Assuming that you have a little bit of idea of Azure cognitive services and you have all the prerequisites needed to start the development activity. As the first step, we need to Create the Computer Vision API Azure Portal.
Create the Computer Vision API Azure Portal
You need to follow the below steps for Creating the Computer Vision API Azure Portal.
Setting up cognitive services account
Log in to the Azure Portal (https://portal.azure.com/)
Once you logged in to the Azure Portal, then Search for the Computer Vision and click on the search result Computer Vision to Create Azure Computer Vision API under the Marketplace as shown below.

Or, there is one more way to get the same option, On the Home page, from the left navigation, click on the + Create a Resource as highlighted below

The next step is, on the New window, search for the Computer Vision, and then you need to click on the search result Computer Vision.

Click o the Create button on the Computer Vision window to create the Computer vision API.

On the Create Computer Vision page, Provide the below details
- Subscription: You need to Choose a valid subscription that you want to use to create the Computer Vision API.
- Resource Group: You can choose your existing Resource Group here. If you don’t have an existing Resource Group, then you can click on the Create new link to create a new Resource Group.
- Region: You need to select the Region for your Azure Resource.
- Name: You will have to provide a unique name for your Azure Cognitive Services Computer vision API.
- Pricing tier: select the Pricing tier based on your business requirement. If you want to check the pricing details, then click on the View full pricing details link to verify the pricing details for your reference. If you are using this For the demo purpose, you can select the Free F0 tier for the Demo purposes where you can utilize 20 calls per minute and 5K calls per month.
Finally, click on the Review + Create button.

Once you clicked on the Review + Create button, it will validate all the information entered by you on the above screen, and then it will show you the Validation Passed message, Now You can able to see the Create button is enabled On the next window as shown below. Now you can cross-check the details entered by you again and then click on the Create button to create the Azure Cognitive Services Computer Vision API.

Now if you can see below that the Deployment is completed successfully and now click on the Go to resource button to navigate to the Azure Cognitive Services computer vision API that you have created above.

Now the important step here is to copy the Key1 value of the Azure Cognitive Services Computer Vision API, To copy the Key1 value, On the Cognitive Services Page, you need to click on the Keys and Endpoint link from the left navigation. you can able to see the Key1 and the Key2 value, As highlighted below, you can click on the Copy button for the Key1 value and keep the Key1 value in a notepad that we will use in our visual studio project on the next steps.
If you don’t want to use the old keys then you can generate the new keys. You can click on the Regenerate Key1 or Regenerate Key2 button to generate a new key for the Key1 or Key2 and you can copy the same.

So we discussed How to create the Computer Vision API Azure Portal. Our first step is over now. We will move to the second step now where we will create a console application using Visual Studio 2019 to generate a thumbnail.
Create a console application using Visual Studio 2019 to generate a thumbnail
Follow the below steps to create a console application using Visual Studio 2019 that will help you to generate a thumbnail.
Create thumbnails using Azure Cognitive Services
Open the Visual Studio 2019 from your local machine and On the Getting started window, click on the Create a new project button to create a new console application.
Choose the project template as Console App (.NET Framework) on the Create a new Project window and then click on the Next button to navigate to the Next window.

On the Configure your new project window, Provide the below details
- Project Name: Provide a unique name for your Console application
- Location: Choose a local path from your local machine where you want to save your project.
- Framework: Choose the latest framework as shown below.
Finally, click on the Create button to create the console application using Visual Studio 2019.

You can able to see the Console application got created successfully

The next step is, we need to add the computer vision NuGet package. To add the NuGet package, Right-click on the Project and then click on the Mange NuGet Packages as shown below.

Now click on the Browse tab and search for Azure Cognitive Services computer vision and once you can able to see the NuGet package as the search result, select the package and then click on the Install button.
On the next window click on the, I Accept button to accept the license agreement, and then it will install the NuGet package in your Project.

Now you need to add the below code in the Program.cs file
The first thing we need to constant variable. One is to keep the key value of the Azure Cognitive Services Computer vision API that we have created above and the other variable to keep the EndPoint of the Computer vision API that we have created above.
const string key = "d38b4303c2774962a9100ed43ff4b76f";
const string endpoint =
"https://mydemocomvision.cognitiveservices.azure.com/";
The other major part is we need to create a method called GenerateThumbnail and add the below code to that method.
public static void GenerateThumbnail(string filename, int width, int
height, bool smartCropping)
{
Task.Run(async () => {
string imgname = Path.GetFileName(filename);
Console.WriteLine($"Thumbnail for image: {imgname}");
Stream thumbnail = await ThumbnailGeneration(
filename, width, height, smartCropping);
string thumbnailPath = string.Format
("{0}\\thumbnail_{1:yyyy-MMM-dd_hh-mm-ss}.jpg",
Path.GetDirectoryName(filename), DateTime.Now);
using (BinaryWriter bw = new BinaryWriter(new
FileStream(thumbnailPath,
FileMode.OpenOrCreate, FileAccess.Write)))
bw.Write(ReadFully(thumbnail));
}).Wait();
}
Creating Thumbnails
If you can see above the method ThumbnailGeneration also called inside the GenerateThumbnail method. The ThumbnailGeneration method should look like below
public static async Task<Stream> ThumbnailGeneration(
string fname, int width, int height, bool smartCropping)
{
Stream thumbnail = null;
ComputerVisionClient clnt = Authenticate(
key, endpoint);
if (File.Exists(fname))
using (Stream stream = File.OpenRead(fname))
thumbnail = await
clnt.GenerateThumbnailInStreamAsync(
width, height, stream, smartCropping);
return thumbnail;
}
Now here is the complete code that you can copy paste in your Program.cs file
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
namespace GenerateThumbnail
{
class Program
{
const string key = "d38b4303c2774962a9100ed43ff4b76f";
const string endpoint =
"https://mydemocomvision.cognitiveservices.azure.com/";
static void Main(string[] args)
{
string imgpath =
@"C:\Users\Bijay\Desktop\VM\Photo.jpg";
GenerateThumbnail(imgpath, 80, 80, true);
Console.ReadKey();
}
public static ComputerVisionClient Authenticate(
string key, string endpoint)
{
ComputerVisionClient clnt =
new ComputerVisionClient(new
ApiKeyServiceClientCredentials(key))
{ Endpoint = endpoint };
return clnt;
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream memorystream = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
memorystream.Write(buffer, 0, read);
}
return memorystream.ToArray();
}
}
public static void GenerateThumbnail(string filename, int width, int
height, bool smartCropping)
{
Task.Run(async () => {
string imgname = Path.GetFileName(filename);
Console.WriteLine($"Thumbnail for image: {imgname}");
Stream thumbnail = await ThumbnailGeneration(
filename, width, height, smartCropping);
string thumbnailPath = string.Format
("{0}\\thumbnail_{1:yyyy-MMM-dd_hh-mm-ss}.jpg",
Path.GetDirectoryName(filename), DateTime.Now);
using (BinaryWriter bw = new BinaryWriter(new
FileStream(thumbnailPath,
FileMode.OpenOrCreate, FileAccess.Write)))
bw.Write(ReadFully(thumbnail));
}).Wait();
}
public static async Task<Stream> ThumbnailGeneration(
string fname, int width, int height, bool smartCropping)
{
Stream thumbnail = null;
ComputerVisionClient clnt = Authenticate(
key, endpoint);
if (File.Exists(fname))
using (Stream stream = File.OpenRead(fname))
thumbnail = await
clnt.GenerateThumbnailInStreamAsync(
width, height, stream, smartCropping);
return thumbnail;
}
}
}
Note: Few important changes you need to make in the above code. Change the Key and endpoint value as per your Azure Cognitive Services. You need to also change the image path based on your image.
For me below is the image which I have kept in the C:\Users\Bijay\Desktop\VM\Photo.jpg path.

Now we have done the code changes and everything is ready. Now the time to run the Console application and see if it is working properly and it’s generating a thumbnail for the image we have provided. What exactly it will do is, it will generate a thumbnail in the same folder path where the image exists and the name will be in the thumbnail_{1:yyyy-MMM-dd_hh-mm-ss}.jpg format.
Press F5 to run the application and if you will go and check in the same image path, you can able to see the thumbnail got generated. For me, the below thumbnail got generated.

This is how you can Create thumbnails using Azure Cognitive Services following the above steps.
Generate thumbnails with Azure Vision API (NodeJS)
Well, Above we have discussed, How To Generate Thumbnails Azure Cognitive Services using C#. Here, we will discuss How to generate thumbnails with Azure Vision API using NodeJS. Follow the below steps to create the Thumbnail.
Like Above, the first step is to Create the Computer Vision API Azure Portal, Since we have created the Azure Computer Vision Service already, so I will use the Key and the Endpoint URL of the same Azure Computer Vision API.
You can use the below code to generate the Thumbnail using the NodeJS
import * as fs from 'fs';
import * as request from 'request';
const Key = '<YOUR_SUBSCRIPTION_KEY>';
const endpoint = 'Your Endpoint URL';
const params = {
smartCropping: true,
height: 400,
width: 400,
};
const options = {
headers: {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key' : Key,
},
qs: params,
uri: `${endpoint}/vision/v2.0/generateThumbnail`,
};
const input = fs.createReadStream('test.jpg');
const output= fs.createWriteStream('test1.jpg');
input.pipe(request.post(options)).pipe(output);
You may also like following the below Articles
- Microsoft Cognitive Services Bing Search Example
- Python 400 client error: Bad request for URL Azure cognitive services
- Azure Cognitive Services Translator Text API Example
- CS1061 C# ‘HttpRequest’ does not contain a definition for ‘Content’ and no accessible extension method ‘Content’ accepting a first argument of type ‘HttpRequest’ could be found
- The specified module ‘ADSync’ was not loaded because no valid module file was found in any module directory error
- How To Convert Text To Speech With Azure Cognitive Services
Wrapping Up
Well, In this article, we have discussed How To Generate Thumbnails Azure Cognitive Services, Create thumbnails using Azure Cognitive Services, Why Thumbnail Service?. We also discussed Creating the Computer Vision API Azure Portal, create a console application using Visual Studio 2019 to generate a thumbnail, Generate thumbnails with Azure Vision API (NodeJS). Hope you have enjoyed this article !!!