How to Convert Images to JPG/JPEG using C# .NET Framework

Cloudmersive
2 min readFeb 5, 2024

Converting any image to JPG format results in a lossy file with controllable output quality. While writing the code to make multiple unique JPG conversions can be challenging in C#, we thankfully don’t need to do all the work ourselves.

Using the below code, we can take advantage of a low-code API solution that allows us to easily convert dozens of unique image formats to JPG format. This supports 100+ image formats (including PDF) and the input format will be automatically detected in the request.

In our request, we can specify the output quality of our resulting JPG images through a quality request parameter by supplying an integer between 1–100 (1 = highest compression; 100 = lowest compression; 75 = default/recommended).

To authorize our request, we’ll just need a free-tier API key. This will allow up to 800 API calls per month with zero additional commitments, ensuring we can easily meet conversion needs at a reasonable scale.

Before we call our function, let’s install the SDK. We can install via NuGet by running the following command in our Package Manager console:

Install-Package Cloudmersive.APIClient.NET.ImageRecognition -Version 3.0.5

Now let’s call the function using the below code examples. We can now include our API key in the appropriate snippet & load in our file for a multipart/form-data request:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.ImageRecognition.Api;
using Cloudmersive.APIClient.NET.ImageRecognition.Client;
using Cloudmersive.APIClient.NET.ImageRecognition.Model;

namespace Example
{
public class ConvertToJpgExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ConvertApi();
var quality = 56; // int? | Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75
var imageFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Image file to perform the operation on. Common file formats such as PNG, JPEG are supported.

try
{
// Convert input image to JPG, JPEG format
byte[] result = apiInstance.ConvertToJpg(quality, imageFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertApi.ConvertToJpg: " + e.Message );
}
}
}
}

Just like that, we’re now able to convert a wide range of image formats to JPG with minimal code.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet

Write a response