How to convert a multi-page document into an array of PNG Thumbnails in C# .NET Framework

Cloudmersive
2 min readJul 28, 2020

The creation of thumbnails is a deceptively difficult task. Generally speaking, this requires splitting your document into individual pages, parsing these, rendering and rasterizing them into images, then resizing the images into thumbnails. That’s a lot of steps, leaving much room for failure, bugs, and general tedium. For a nice simplified solution to this problem, we will be turning to an API that can get this sorted for you automatically.

First we will need to grab out API client package via NuGet:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.2.8

Next comes our function call for converting to thumbnails:

using System;using System.Diagnostics;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;namespace Example{public class ConvertDocumentAutodetectToThumbnailsAdvancedExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new ConvertDocumentApi();var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.var pages = 56;  // int? | Optional; Specify how many pages of the document will be converted to thumbnails. Default is 1 page. (optional)var maxWidth = 56;  // int? | Optional; Maximum width of the output thumbnail - final image will be as large as possible while less than or equal to this width. Default is 128. (optional)var maxHeight = 56;  // int? | Optional; Maximum height of the output thumbnail - final image will be as large as possible while less than or equal to this width. Default is 128. (optional)var extension = extension_example;  // string | Optional; Specify the file extension of the inputFile. This will improve the response time in most cases. Also allows unsupported files without extensions to still return a corresponding generic icon. (optional)try{// Convert File to Thumbnail Image ObjectAutodetectToThumbnailsResult result = apiInstance.ConvertDocumentAutodetectToThumbnailsAdvanced(inputFile, pages, maxWidth, maxHeight, extension);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentAutodetectToThumbnailsAdvanced: " + e.Message );}}}}

Be sure to set pages to be the total number of pages of the document that you would like as thumbnails. And that’s really all there is to it. Call this with your document file and you will have an array of download links corresponding to your images.

--

--

Cloudmersive

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