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 Cell
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:
Range |
Description |
---|---|
Resolution | Defines the image resolution (in DPI). |
Scale | Specifies how to scale worksheet content before it is exported to an image. |
Background |
Allows you to fill the image background with a color. |
Export |
Specifies whether to include row and column headings in the output image. |
Export |
Specifies whether to include drawing objects (charts, shapes, and pictures) in the output image. |
Export |
Specifies whether to include cell gridlines in the output image. |
Gridline |
Defines the gridline color. |
Black |
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);
}