Back to Devexpress

How to: Generate a Thumbnail Image from a Chart Sheet

officefileapi-403379-spreadsheet-document-api-examples-charts-how-to-generate-a-thumbnail-from-a-chart-sheet.md

latest5.6 KB
Original Source

How to: Generate a Thumbnail Image from a Chart Sheet

  • Jan 27, 2022
  • 3 minutes to read

The Spreadsheet Document API allows you to create thumbnails for chart sheets. A thumbnail is a small image that you can insert into a document, presentation, or website to display a preview of chart sheet content.

Call the ChartSheetExtensions.CreateThumbnail extension method for a ChartSheet object to generate a thumbnail image of the specified size from a chart sheet. Pass an ImageFileFormat enumeration member to this method to set the thumbnail image format.

Important

The ChartSheetExtensions class is defined in the DevExpress.Docs.v25.2.dll assembly. Add this assembly to your project to use chart sheet extensions. You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this assembly in production code.

If a chart sheet is bigger than the thumbnail image, the chart sheet is cropped to fit the thumbnail. If the chart sheet is smaller, the remaining space is filled with the default background color.

The code sample below saves a chart sheet as an image.

csharp
using DevExpress.Spreadsheet;
// ...

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

    // Access an active chart sheet.
    ChartSheet chartSheet = workbook.ChartSheets.ActiveChartSheet;

    // Create a thumbnail.
    if (chartSheet != null)
        chartSheet.CreateThumbnail("Chart_sheet_Thumbnail.png", ImageFileFormat.Png, 920, 670);
}
vb
Imports DevExpress.Spreadsheet
' ...

' Create a new Workbook object.
Using workbook As New Workbook()
    ' Load a workbook from a file.
    workbook.LoadDocument("VariableCosts.xlsx", DocumentFormat.Xlsx)

    ' Access an active chart sheet.
    Dim chartSheet As ChartSheet = workbook.ChartSheets.ActiveChartSheet

    ' Create a thumbnail.
    If chartSheet IsNot Nothing Then
        chartSheet.CreateThumbnail("Chart_sheet_Thumbnail.png", ImageFileFormat.Png, 920, 670)
    End If
End Using

Customize Thumbnail Settings

Create a SheetThumbnailOptions class instance and pass it to the ChartSheetExtensions.CreateThumbnail method to specify thumbnail options. The following options are available:

SheetThumbnailOptions PropertyDescription
BackgroundColorSpecifies the background color for the output image.
ResolutionSpecifies the image resolution (in DPI).
ScaleSpecifies how to scale content of a chart sheet before it is saved as an image. The Scale property has no effect if the Stretch property is set to true.
StretchSpecifies whether a chart sheet is stretched to fit the output image size.

The code sample below specifies thumbnail options and generates a thumbnail.

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

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

    // Access an active chart sheet.
    ChartSheet chartSheet = workbook.ChartSheets.ActiveChartSheet;

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

    // Generate a thumbnail.
    if (chartSheet != null)
        chartSheet.CreateThumbnail("Chart_sheet_Thumbnail.png", ImageFileFormat.Png, 800, 600, thumbnailOptions);
}
vb
Imports DevExpress.Spreadsheet
Imports System.Drawing
' ...

' Create a new Workbook object.
Using workbook As New Workbook()
    ' Load a workbook from a file.
    workbook.LoadDocument("VariableCosts.xlsx", DocumentFormat.Xlsx)

    ' Access an active chart sheet.
    Dim chartSheet As ChartSheet = workbook.ChartSheets.ActiveChartSheet

    ' Specify thumbnail options.
    Dim thumbnailOptions = New SheetThumbnailOptions With {
      .Resolution = 192,
      .Scale = 40,
      .BackgroundColor = Color.FromArgb(&HF2, &HF2, &HF2)
    }

    ' Generate a thumbnail.
    If chartSheet IsNot Nothing Then
        chartSheet.CreateThumbnail("Chart_sheet_Thumbnail.png", ImageFileFormat.Png, 800, 600, thumbnailOptions)
    End If
End Using