How to convert an EML file into a Thumbnail PNG Image in C# .NET Framework

Creating thumbnails is an excellent way to improve your user experience. Unfortunately, generating thumbnails from non-image files such as EMLs is actually quite tedious and even a bit challenging. First it is necessary to parse the EML into its constituent HTML, then render and rasterize that into an image, and finally resize that. To set all of this up from scratch takes quite a while and is more than a little prone to bugs. Instead, I will be showing you a nice simple solution to this problem.

First we import our client library from NuGet using this command here:

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

Now we can go forward with our function call, which is going to look in essence like this here:

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 there you go! Thumbnails at your disposal. This function will actually automatically detect the incoming format, making it great for handling a wide range of different document types on its own.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store