Create Transparent Image of Excel Worksheet
Contents
[
Hide
]
Sometimes, you need to generate the image of your worksheet as a transparent image. You want to apply transparency to all cells which have no fill colors. Aspose.Cells provides the ImageOrPrintOptions.Transparent property to apply transparency to the worksheet image. When this property is false, then cells with no fill colors are drawn with white color and when it is true, cells with no fill colors are drawn transparent.
In the following worksheet image, transparency has not been applied. The cells with no fill colors are drawn white.
Output without transparency: the cell background is white |
---|
While, in the following worksheet image, transparency has been applied. The cells with no fill colors are transparent.
Output with transparency enabled |
---|
The following sample code generates a transparent image from an Excel worksheet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Create workbook object from source file | |
Workbook wb = new Workbook(sourceDir + "sampleCreateTransparentImage.xlsx"); | |
// Apply different image or print options | |
var imgOption = new ImageOrPrintOptions(); | |
imgOption.ImageType = Drawing.ImageType.Png; | |
imgOption.HorizontalResolution = 200; | |
imgOption.VerticalResolution = 200; | |
imgOption.OnePagePerSheet = true; | |
// Apply transparency to the output image | |
imgOption.Transparent = true; | |
// Create image after apply image or print options | |
var sr = new SheetRender(wb.Worksheets[0], imgOption); | |
sr.ToImage(0, outputDir + "outputCreateTransparentImage.png"); |