How to Convert Images to PNG Format using C# .NET Framework

Cloudmersive
2 min readFeb 5, 2024

--

Storing and/or sharing images in PNG format has myriad benefits. Unfortunately, writing C# code to convert unique image formats to PNG can be a bit of a hassle.

Thankfully, using the ready-to-run C# code below, we can take advantage of a free API that allows us to convert over 100+ unique image formats to PNG format. This even supports raster PDF documents, so we can easily cover most of our common image format conversion needs.

Before we structure our API call with code examples, let’s start by obtaining a free API key to authorize our API calls. This will allow us to make up to 800 API calls per month with zero commitments (once we reach our monthly limit, it’ll reset for the following month).

With our API key ready to go, let’s go ahead and install the SDK. We can run the following command in our Package Manager console to install via NuGet:

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

Lastly, let’s call the function using the below code. Let’s remember to authorize our API call with our free API key:

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 ConvertToPngExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ConvertApi();
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 PNG format
byte[] result = apiInstance.ConvertToPng(imageFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertApi.ConvertToPng: " + e.Message );
}
}
}
}

Now we can easily convert dozens of images to PNG with minimal code.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet