How to: Save a Cell Range as an Image
- 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.v24.1.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:
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);
}
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 Property | Description |
---|---|
Resolution | Defines the image resolution (in DPI). |
Scale | Specifies how to scale worksheet content before it is exported to an image. |
BackgroundColor | Allows you to fill the image background with a color. |
ExportHeadings | Specifies whether to include row and column headings in the output image. |
ExportDrawingObjects | Specifies whether to include drawing objects (charts, shapes, and pictures) in the output image. |
ExportGridlines | Specifies whether to include cell gridlines in the output image. |
GridlineColor | Defines the gridline color. |
BlackAndWhite | Allows 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:
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);
}