DEV Community

Cover image for 4 Simple Steps to Create Thumbnail Images for Excel Documents in C#
Jollen Moyani for Syncfusion, Inc.

Posted on • Originally published at syncfusion.com on

4 Simple Steps to Create Thumbnail Images for Excel Documents in C#

In today’s digital age, visual representation is crucial in capturing attention and conveying information effectively. Whether working on a personal project or developing professional software, creating visually appealing thumbnail images can significantly enhance the user experience and facilitate more straightforward navigation.

This article will show you how to create a thumbnail image for an Excel document using the Syncfusion .NET Core Excel (XlsIO) Library in C#.

Creating thumbnail images for Excel documents using C

Follow these steps to create a thumbnail image for an Excel document using the Syncfusion .NET Excel Library:

Note: If you are new to our Excel Library, our Getting Started guide is highly recommended.

1.First, open Visual Studio and create a new .NET Core console application. Create a .NET Core console application
2.Then, install the latest version of the Syncfusion.XlsIORenderer.Net.Core NuGet package. Install the Syncfusion.XlsIORenderer.Net.Core NuGet package
3.Add the following code to the Program.cs file to convert an Excel document to an image.

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    FileStream inputStream = new FileStream("../../../Data/InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(inputStream);
    IWorksheet sheet = workbook.Worksheets[0];

    //Initialize XlsIORenderer.
    application.XlsIORenderer = new XlsIORenderer();

    //Convert to image.
    MemoryStream outputStream = new MemoryStream();
    sheet.ConvertToImage(sheet.UsedRange, outputStream);
}
Enter fullscreen mode Exit fullscreen mode

4.Then, add the following code to resize the image to thumbnail size.

//Resize image to thumbnail size.
System.Drawing.Image image = System.Drawing.Image.FromStream(outputStream);
System.Drawing.Image thumbnail = image.GetThumbnailImage(100, 100, () => false, IntPtr.Zero);

//Save image.
thumbnail.Save("Image.png", System.Drawing.Imaging.ImageFormat.Png);
Enter fullscreen mode Exit fullscreen mode

Following is the complete code of the Program.cs file.

using System;
using System.IO;
using Syncfusion.XlsIO;
using Syncfusion.XlsIORenderer;

namespace Excel_to_Image
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Xlsx;
                FileStream inputStream = new FileStream("../../../Data/InputTemplate.xlsx", FileMode.Open, FileAccess.Read);
                IWorkbook workbook = application.Workbooks.Open(inputStream);
                IWorksheet sheet = workbook.Worksheets[0];

                //Initialize XlsIORenderer.
                application.XlsIORenderer = new XlsIORenderer();

                //Convert to image.
                MemoryStream outputStream = new MemoryStream();
                sheet.ConvertToImage(sheet.UsedRange, outputStream);

                //Resize image to thumbnail size.
                System.Drawing.Image image = System.Drawing.Image.FromStream(outputStream);
                System.Drawing.Image thumbnail = image.GetThumbnailImage(100, 100, () => false, IntPtr.Zero);

                //Save the image.
                thumbnail.Save("Image.png", System.Drawing.Imaging.ImageFormat.Png);
            }

            System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo = new System.Diagnostics.ProcessStartInfo("Image.png")
            {
                UseShellExecute = true
            };
            process.Start();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Refer to the following images.

Input Excel document

Input Excel document

Thumbnail image for the input Excel document

Thumbnail image for the input Excel document

GitHub reference

You can download the example for creating thumbnail images for Excel documents on this GitHub page.

Conclusion

Thanks for reading! In this blog, we’ve seen how to create thumbnail images for Excel documents using the Syncfusion .NET Core Excel Library in C#. Take a moment to peruse the documentation, where you’ll find other features with accompanying code samples.

Using this library, you can also export Excel data to PDF, data tables, CSV, TSV, HTML, collections of objects, ODS, JSON, and other file formats.

Are you already a Syncfusion subscriber? You can download the product setup here. If you’re not a Syncfusion subscriber, you can download a free 30-day trial to check out Essential Studio’s features.

Please let us know in the comments section below if you have any questions about these features. You can also contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!

Related blogs

Top comments (0)