How to convert any document into a Thumbnail PNG Image in C# .NET Framework
2 min readJul 26, 2020
The process that is required to turn a document file into a thumbnail is a surprisingly long and tedious one. First, one needs to detect the incoming format, then parse it, render, rasterize, split into pages, and finally resize the images. A ton of work is required to get this running smoothly. For C# users, however, I have a much simpler alternative method that you can try.
First we install our client using this code here:
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.2.8
Next we go ahead with our function call:
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 ConvertDocumentAutodetectToThumbnailExample{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 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 Imagebyte[] result = apiInstance.ConvertDocumentAutodetectToThumbnail(inputFile, maxWidth, maxHeight, extension);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentAutodetectToThumbnail: " + e.Message );}}}}
Done! Easy.