.NET quick start

Last updated: Aug-26-2022

This quick start is intended to let you quickly try using several common Cloudinary features. It doesn't necessarily employ coding best practices and the code you create here isn't intended for production.

You can perform this quick start in a code sandbox or in a clean project in the dev environment of your choice.

You can also view the completed code of this quick start in a GitHub repo.

Tip
If you aren't familiar with Cloudinary, you may want to first take a look at the Developer get started guide for a high-level overview of integrating Cloudinary into your code, and an introduction to the main concepts.

You may also find our Glossary helpful to understand Cloudinary-specific terminology.

Prerequisites

To perform the steps in this quick start, you'll need:

1. Set up and configure the library

Add CloudinaryDotNet and dotenv.net using the Nuget Package Manager or if using the Package Manager Console, run:

Copy to clipboard
Install-Package CloudinaryDotNet
Install-Package dotenv.net

Then, in your project, create a file called .env and add the following line with your Cloudinary environment variable:

.env

Copy to clipboard
# Copy and paste your API environment variable
# =============================================

CLOUDINARY_URL=cloudinary://<API_KEY>:<API_SECRET>@<CLOUD_NAME>

Important
When writing your own applications, follow your organization's policy on storing secrets and don't expose your API secret.

In your code, include the following CloudinaryDotNet libraries as well as the dotenv.net library in your project.

You can then load your Cloudinary credentials from your .env file as shown below. Copy and paste the following code into your Program.cs file:

Program.cs

Copy to clipboard
// Import the required packages
//==============================

using CloudinaryDotNet;
using CloudinaryDotNet.Actions;
using dotenv.net;

// Set your Cloudinary credentials
//=================================

DotEnv.Load(options: new DotEnvOptions(probeForEnv: true));
Cloudinary cloudinary = new Cloudinary(Environment.GetEnvironmentVariable("CLOUDINARY_URL"));
cloudinary.Api.Secure = true;

2. Upload an image

Copy and paste the following code into your Program.cs file:

Program.cs (continued)

Copy to clipboard
// Upload an image and log the response to the console
//=================

var uploadParams = new ImageUploadParams()
{
    File = new FileDescription(@"https://cloudinary-devs.github.io/cld-docs-assets/assets/images/cld-sample.jpg"),
    UseFilename = true,
    UniqueFilename = false,
    Overwrite = true
};
var uploadResult = cloudinary.Upload(uploadParams);
Console.WriteLine(uploadResult.JsonObj);

3. Get details of the image

Program.cs (continued)

Copy to clipboard
// Get details of the image and run quality analysis
//==============================

var getResourceParams = new GetResourceParams("cld-sample")
{
    QualityAnalysis = true
};
var getResourceResult = cloudinary.GetResource(getResourceParams);
var resultJson = getResourceResult.JsonObj;

// Log quality analysis score to the console
Console.WriteLine(resultJson["quality_analysis"]);

4. Transform the uploaded image

Program.cs (continued)

Copy to clipboard
// Transform the uploaded asset and generate a URL and image tag
//==============================

var myTransformation = cloudinary.Api.UrlImgUp.Transform(new Transformation()
    .Width(300).Crop("scale").Chain()
    .Effect("cartoonify"));

var myUrl = myTransformation.BuildUrl("cld-sample");
var myImageTag = myTransformation.BuildImageTag("cld-sample");

// Log the URL of the transformed asset to the console
Console.WriteLine(myUrl);

// Log the image tag for the transformed asset to the console
Console.WriteLine(myImageTag);

5. Run your code

Run your code by clicking the Run button in Visual Studio or .NET fiddle.

You can use the returned image tag to display the image on your website. For now, copy and paste the URL to see the transformed image in the browser:

Transformed cld-sample image

View the completed code

You can find the full code example for this on GitHub.

Next steps

  • Learn more about the .NET SDK by visiting the other pages in this SDK guide.
  • Get comprehensive details about Cloudinary features and capabilities:

✔️ Feedback sent!

Rate this page: