.NET Spreadsheet (WinForms, WPF, Office File API) – Create Thumbnail Images for Worksheets and Chart Sheets (v21.2)

Office-Inspired Products
21 February 2022

Our Spreadsheet Document API v21.2 allows you to generate thumbnails from worksheets and chart sheets. A thumbnail is a small image that you can insert into a document, presentation, or website to display a preview of worksheet or chart sheet content.

As you may recall, we added the ability to save a cell range as an image in our previous major release (v21.1). For background information on our implementation, please review the following blog post:

.NET Spreadsheet (WinForms, WPF, Office File API) – Export and Copy Cell Ranges as Images (v21.1).

With v21.2, we added a new API to save a worksheet or chart sheet as a thumbnail image. The following image formats are supported:

  • PNG
  • JPEG
  • BMP
  • TIFF
  • GIF
  • EMF (not available for Linux and macOS)
  • WMF (not available for Linux and macOS)

Note: You need a license for the DevExpress Office File API Subscription or the DevExpress Universal Subscription to use this API in production code. If you’d like to purchase the Office File API or if you wish to upgrade your current subscription to DevExpress Universal, write to us at clientservices@devexpress.com.

Create a Thumbnail Image for a Worksheet

To generate a thumbnail image for a worksheet, call the WorksheetExtensions.CreateThumbnail extension method for a Worksheet object. The generated thumbnail image includes worksheet cells (from “A1” to the bottom-right cell) with data or formatting (excluding hidden rows and columns). Worksheet row and column headers are also exported to the thumbnail.

You can use the WorksheetThumbnailOptions class instance to specify the following thumbnail settings:

  • Define image resolution (in DPI)
  • Scale worksheet content before thumbnail generation
  • Change image background color
  • Set row and column indexes from which thumbnail generation should start
  • Stretch a worksheet to fit output image size

The code sample below specifies thumbnail options and generates a thumbnail image from a worksheet.

using DevExpress.Spreadsheet;
using System.Drawing;
// ...

// Create Workbook object.
using (var workbook = new Workbook())
{
    // Load workbook from file.
    workbook.LoadDocument("TopTradingPartners.xlsx");

    // Access worksheet.
    var worksheet = workbook.Worksheets["Trading Partners"];

    // Specify thumbnail options.
    var thumbnailOptions = new WorksheetThumbnailOptions
    {
        Resolution = 192,
        Scale = 80,
        ColumnOffset = 1,
        RowOffset = 1,
        BackgroundColor = Color.FromArgb(0xF2, 0xF2, 0xF2)
    };

    // Create thumbnail image.
    worksheet.CreateThumbnail("Worksheet_Thumbnail.png", 
        ImageFileFormat.Png, 1600, 900, thumbnailOptions);
}

Create a Thumbnail Image for a Chart Sheet

Call the ChartSheetExtensions.CreateThumbnail extension method for a ChartSheet object to generate a thumbnail image for a chart sheet. Use SheetThumbnailOptions to customize thumbnail settings as needed. You can specify thumbnail resolution (in DPI), define background color, scale chart sheet content before export, or stretch the chart sheet to fit output image size.

The code sample below specifies thumbnail options and creates a thumbnail image for a chart sheet.

using DevExpress.Spreadsheet;
using System.Drawing;
// ...

// Create Workbook object.
using (var workbook = new Workbook())
{
    // Load workbook from file.
    workbook.LoadDocument("VariableCosts.xlsx");

    // Access chart sheet.
    var chartSheet = workbook.ChartSheets["Variable Costs"];

    // Specify thumbnail options.
    var thumbnailOptions = new SheetThumbnailOptions
    {
        Resolution = 192,
        Scale = 40,
        BackgroundColor = Color.FromArgb(0xF2, 0xF2, 0xF2)
    };

    // Create thumbnail image.
    chartSheet.CreateThumbnail("Chart_sheet_Thumbnail.png", 
        ImageFileFormat.Png, 800, 600, thumbnailOptions);
}

Thumbnail generation is also available for the WinForms Spreadsheet and WPF Spreadsheet controls. Add the DevExpress.Docs.v21.2.dll assembly to your project to use our CreateThumbnail extension methods. Once again, please remember that you must own an active Office File API or Universal Subscription to use this assembly in production code.

Tell Us What You Think

As always, we welcome your feedback. Please feel free to leave comments below or contact us via the DevExpress Support Center.

Free DevExpress Products – Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We’ll be happy to follow-up.
Grigore Dolghin
Grigore Dolghin
Thank you for fine this addition to an already great product. I am using spreadsheets heavily in my apps, the thumbnail will make the look even better.
3 March 2022

Please login or register to post comments.