WorkbookRender.ToImage

ToImage(Stream)

Render whole workbook as Tiff Image to stream.

public void ToImage(Stream stream)
ParameterTypeDescription
streamStreamthe stream of the output image

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using Aspose.Cells.Drawing;
    using Aspose.Cells.Rendering;
    using System;
    using System.IO;

    public class WorkbookRenderMethodToImageWithStreamDemo
    {
        public static void Run()
        {
            // Create a new workbook with sample data
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];
            worksheet.Cells["A1"].Value = "Aspose.Cells ToImage Stream Demo";

            // Configure image rendering options
            ImageOrPrintOptions options = new ImageOrPrintOptions();
            options.ImageType = ImageType.Png; // Fixed namespace AsposeCellsExamples

            // Create output directory if it doesn't exist
            string outputDir = "output/";
            Directory.CreateDirectory(outputDir);

            // Create a file stream for image output
            string imagePath = Path.Combine(outputDir, "workbook_render.png");
            using (FileStream imageStream = new FileStream(imagePath, FileMode.Create))
            {
                try
                {
                    // Initialize workbook renderer and convert to image
                    WorkbookRender renderer = new WorkbookRender(workbook, options); // Removed using statement
                    renderer.ToImage(imageStream);

                    Console.WriteLine($"Workbook rendered successfully to: {imagePath}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error during rendering: {ex.Message}");
                }
            }

            // Save the original workbook for reference
            workbook.Save(Path.Combine(outputDir, "source_workbook.xlsx"));
        }
    }
}

See Also


ToImage(string)

Render whole workbook as Tiff Image to a file.

public void ToImage(string filename)
ParameterTypeDescription
filenameStringthe filename of the output image

Examples

using System;
using Aspose.Cells;
using Aspose.Cells.Rendering;
using Aspose.Cells.Drawing;

namespace AsposeCellsExamples
{
    public class WorkbookRenderMethodToImageWithStringDemo
    {
        public static void Run()
        {
            // Create a sample workbook
            Workbook wb = new Workbook();
            Worksheet ws = wb.Worksheets[0];
            ws.Cells["A1"].PutValue("Test ToImage Method");
            
            // Set image options
            ImageOrPrintOptions imgOpt = new ImageOrPrintOptions();
            imgOpt.ImageType = ImageType.Tiff;
            imgOpt.TiffCompression = TiffCompression.CompressionLZW;
            imgOpt.HorizontalResolution = 300;
            imgOpt.VerticalResolution = 300;

            // Create workbook render and save to image
            WorkbookRender wbRender = new WorkbookRender(wb, imgOpt);
            string outputPath = "output.tiff";
            wbRender.ToImage(outputPath);
            
            Console.WriteLine("Workbook rendered to image: " + outputPath);
        }
    }
}

See Also


ToImage(int, string)

Render certain page to a file.

public void ToImage(int pageIndex, string fileName)
ParameterTypeDescription
pageIndexInt32indicate which page is to be converted
fileNameStringfilename of the output image

Examples

using System;
using Aspose.Cells;
using Aspose.Cells.Rendering;

namespace AsposeCellsExamples
{
    public class WorkbookRenderMethodToImageWithInt32StringDemo
    {
        public static void Run()
        {
            // Create a new workbook with sample data
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];
            worksheet.Cells["A1"].PutValue("Sample Data");
            
            // Create image options
            ImageOrPrintOptions options = new ImageOrPrintOptions
            {
                OnePagePerSheet = true
            };

            // Create workbook renderer
            WorkbookRender renderer = new WorkbookRender(workbook, options);

            // Save each page as image
            for (int i = 0; i < renderer.PageCount; i++)
            {
                renderer.ToImage(i, $"output_page_{i}.png");
            }
        }
    }
}

See Also


ToImage(int, Stream)

Render certain page to a stream.

public void ToImage(int pageIndex, Stream stream)
ParameterTypeDescription
pageIndexInt32indicate which page is to be converted
streamStreamthe stream of the output image

See Also


ToImage(int)

Render certain page to a Bitmap object.

public Bitmap ToImage(int pageIndex)
ParameterTypeDescription
pageIndexInt32indicate which page is to be converted

Return Value

the bitmap object of the page

See Also