Back to Devexpress

How to: Save a Cell Range as an Image

officefileapi-403109-spreadsheet-document-api-examples-cells-how-to-save-a-cell-range-as-an-image.md

latest5.0 KB
Original Source

How to: Save a Cell Range as an Image

  • Sep 19, 2023
  • 2 minutes to read

Call the CellRangeExtensions.ExportToImage extension method for a CellRange object to save a cell range as an image. Pass an ImageFileFormat enumeration member to this method to specify the output image format.

Important

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

The following code snippet demonstrates how to export a cell range as an image:

csharp
using DevExpress.Spreadsheet;
// ...

using (Workbook workbook = new Workbook())
{
    workbook.LoadDocument("InvestmentPortfolio.xlsx", DocumentFormat.Xlsx);
    Worksheet worksheet = workbook.Worksheets.ActiveWorksheet;
    worksheet.Range["B1:I25"].ExportToImage("RangeImage.png", ImageFileFormat.Png);
}
vb
Imports DevExpress.Spreadsheet
' ...

Using workbook As New Workbook()
    workbook.LoadDocument("InvestmentPortfolio.xlsx", DocumentFormat.Xlsx)
    Dim worksheet As Worksheet = workbook.Worksheets.ActiveWorksheet
    worksheet.Range("B1:I25").ExportToImage("RangeImage.png", ImageFileFormat.Png)
End Using

Customize Export Settings

Create a RangeImageOptions class instance and pass it to the CellRange.ExportToImage method to specify export options. The following options are available:

RangeImageOptions PropertyDescription
ResolutionDefines the image resolution (in DPI).
ScaleSpecifies how to scale worksheet content before it is exported to an image.
BackgroundColorAllows you to fill the image background with a color.
ExportHeadingsSpecifies whether to include row and column headings in the output image.
ExportDrawingObjectsSpecifies whether to include drawing objects (charts, shapes, and pictures) in the output image.
ExportGridlinesSpecifies whether to include cell gridlines in the output image.
GridlineColorDefines the gridline color.
BlackAndWhiteAllows you to save a cell range as a black and white image.

The following example demonstrates how to save a cell range as an image and define export options to create the following image:

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

using (Workbook workbook = new Workbook())
{
    workbook.LoadDocument("InvestmentPortfolio.xlsx", DocumentFormat.Xlsx);
    Worksheet worksheet = workbook.Worksheets.ActiveWorksheet;
    var exportOptions = new RangeImageOptions
    {
        Resolution = 192,
        ExportHeadings = true,
        ExportGridlines = true,
        GridlineColor = Color.Gray,
        BackgroundColor = Color.FromArgb(0xF2, 0xF2, 0xF2)
    };
    worksheet.Range["B1:I25"].ExportToImage("RangeImage.png", ImageFileFormat.Png, exportOptions);
}
vb
Imports DevExpress.Spreadsheet
Imports System.Drawing
' ...

Using workbook As New Workbook()
    workbook.LoadDocument("InvestmentPortfolio.xlsx", DocumentFormat.Xlsx)
    Dim worksheet As Worksheet = workbook.Worksheets.ActiveWorksheet
    Dim exportOptions As New RangeImageOptions With {
        .Resolution = 192,
        .ExportHeadings = True,
        .ExportGridlines = True,
        .GridlineColor = Color.Gray,
        .BackgroundColor = Color.FromArgb(&HF2, &HF2, &HF2)
    }
    worksheet.Range("B1:I25").ExportToImage("RangeImage.png", ImageFileFormat.Png, exportOptions)
End Using