Articles in this section
Category / Section

How to convert entire Excel workbook into TIFF image?

2 mins read

Syncfusion WinForms XlsIO supports converting entire Excel workbook to TIFF image with the help of MagickImage.NET, a third party library, which supports to read and write multi-page TIFF images.

The following code snippet explains this.

C#

using (ExcelEngine excelEngine = new ExcelEngine())
{
    IApplication application = excelEngine.Excel;
    application.DefaultVersion = ExcelVersion.Xlsx;
    application.XlsIORenderer = new XlsIORenderer();
 
    FileStream fileStream = new FileStream("../../../Data/ExportData.xlsx", FileMode.Open, FileAccess.Read);
    IWorkbook workbook = application.Workbooks.Open(fileStream);
    fileStream.Dispose();
 
    var outputStream = new MemoryStream();
    using (MagickImageCollection magicImageCollection = new MagickImageCollection())
    {
        foreach (IWorksheet sheet in workbook.Worksheets)
        {
            Stream stream = new MemoryStream();
            IRange used = sheet.UsedRange;
            ExportImageOptions exportImageOptions = new ExportImageOptions();
            exportImageOptions.ScalingMode = ScalingMode.Normal;
            exportImageOptions.ImageFormat = ExportImageFormat.Png;
            sheet.ConvertToImage(used.Row, used.Column, used.LastRow, used.LastColumn, exportImageOptions, stream);
            stream.Position = 0;
 
            MagickImage magickImage = new MagickImage(stream);
            magickImage = CompressImage(magickImage, 10);
            magicImageCollection.Add(magickImage);
        }
        var outputData = magicImageCollection.ToByteArray(MagickFormat.Tif);
        outputStream.Write(outputData, 0, outputData.Length);
    }
    File.WriteAllBytes("Output.tiff", outputStream.ToArray());
}
 
static MagickImage CompressImage(MagickImage image, int quality)
{
    MagickImage newmagickImage = null;
    using (MemoryStream memoryStream1 = new MemoryStream())
    {
        image.Quality = quality;
        image.Write(memoryStream1, MagickFormat.Png);
        memoryStream1.Position = 0;
        ImageOptimizer imageOptimizer = new ImageOptimizer();
        imageOptimizer.LosslessCompress(memoryStream1);
        memoryStream1.Position = 0;
        newmagickImage = new MagickImage(memoryStream1);
    }
 
    return newmagickImage;
}

 

A complete working sample can be downloaded from Excel-TIFF-Conversion.zip

Conclusion

I hope you enjoyed learning about how to convert entire Excel workbook into TIFF image.

You can refer to our WinForms XIsIo's feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms XIsIo documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms XIsIo and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied