How to convert a DOCX document into a Thumbnail PNG Image in C# .NET Framework

Cloudmersive
2 min readJul 28, 2020

The general process for converting a document (such as DOCX) into a thumbnail image is a surprisingly complex one. Essentially it requires first parsing the document’s format, separating the first page, converting this into an image through rendering and rasterization, then resizing said image. While none of these steps poses an extreme challenge on its own, the combination can prove deadly to your endeavors. Instead of dealing with that potential time sink, I have a much simpler solution that I will demonstrate for you now.

To get things done quickly, we will be using an API, so let’s install its client files as our first step.

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

Then we call our function like you see here in this example code:

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 );}}}}

And that’s really all there is to it! Super simple.

--

--

Cloudmersive

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