Managing compression while converting Excel worksheet to TIFF using Aspose.Cells for .NET in C#

Hello


When testing tiff images that are output from our system that uses Aspose.Cells I noticed that it seems that whatever compression I use only LZW is ever used which causes my tiff images not to pass the test. I am using DPF Manager (http://www.preforma-project.eu/dpfmanager-download.html) to test the tiff images.

Our system converts many types of files into Tiff but I have only so far encountered this with Excel files/Aspose.Cells library.

My code is as follows:

private byte[] convertFromExcel(MemoryStream instream)
{
MemoryStream outstream = new MemoryStream();
Aspose.Cells.License licenceCells = new Aspose.Cells.License();
licenceCells.SetLicense(“Aspose.Total.lic”);

Workbook book = new Workbook(instream);
int counter = 1;
Bitmap bitmap = null;

ImageCodecInfo codecInfo = null;
foreach (ImageCodecInfo info in ImageCodecInfo.GetImageEncoders())
{
if (info.MimeType == “image/tiff”)
{
codecInfo = info;
break;
}
}
EncoderParameters ep = new EncoderParameters(1);
System.Drawing.Imaging.Encoder encoder = System.Drawing.Imaging.Encoder.SaveFlag;
foreach (Worksheet sheet in book.Worksheets)
{
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();

imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Tiff;
imgOptions.HorizontalResolution = 300;
imgOptions.VerticalResolution = 300;
imgOptions.OnePagePerSheet = false;
imgOptions.TiffCompression = Aspose.Cells.Rendering.TiffCompression.CompressionNone; // Tiff compression in the file is always LZW

SheetRender sr = new SheetRender(sheet, imgOptions);
ImageCodecInfo ici = GetEncoder(ImageFormat.Tiff);

EncoderParameter parameter = new EncoderParameter(encoder, (long)EncoderValue.MultiFrame);
ep.Param[0] = parameter;

if (counter < 2)
{
bitmap = sr.ToImage(0);
if (bitmap == null)
{
throw new Exception(“Bitmap from excel sheet is null. Is the excel document empty?”);
}
bitmap.SetResolution(300F, 300F);
bitmap.Save(outstream, codecInfo, ep);
}
else
{
ep.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.FrameDimensionPage);
Bitmap bmap = sr.ToImage(0);
if (bmap != null)
{
bitmap.SaveAdd(bmap, ep);
}
}
if (counter == book.Worksheets.Count - 1)
{
ep.Param[0] = new EncoderParameter(encoder, (long)EncoderValue.Flush);
bitmap.SaveAdd(ep);
}
counter++;
}
return outstream.ToArray();
}

Do you see anything wrong here or is there a bug in the system? We are using Aspose.Cells version 8.3.1.0

Hi,


Thanks for your posting and using Aspose.Cells.

You are using an older version and there might be a bug inside it. You must test your issue with the latest version and let us know your feedback. If the issue still occurs, then please provide us a console application project (instead of code) and also let us know how do you know if the generated image is using LZW or any other compression. It will help us replicate and investigate the issue quickly and we will log it in our database for a fix.

FYI:
Please download and try the following latest fix and let us know your feedback.

Hi,


Thanks for your posting and using Aspose.Cells.

We have looked into this issue further and tested your issue with the following sample code using the latest version given in the previous post and with the attached sample file and found all of the tiff outputs are of different sizes. If the code always generates the LZW output, then their sizes would be same but you see, their sizes are like

  • 7 KB
  • 4 KB
  • 5 KB
  • 1472 KB
  • 10 KB

So it means, the latest version is generating tiff images with correct compression.

C#
Workbook wb = new Workbook(“sample.xlsx”);

Worksheet ws = wb.Worksheets[0];

ImageOrPrintOptions opts = new ImageOrPrintOptions();
opts.TiffCompression = TiffCompression.CompressionNone;
opts.OnePagePerSheet = true;
opts.SaveFormat = SaveFormat.TIFF;

SheetRender sr = new SheetRender(ws, opts);
sr.ToImage(0, “CompressionNone.tiff”);

opts.TiffCompression = TiffCompression.CompressionCCITT3;
sr = new SheetRender(ws, opts);
sr.ToImage(0, “CompressionCCITT3.tiff”);

opts.TiffCompression = TiffCompression.CompressionCCITT4;
sr = new SheetRender(ws, opts);
sr.ToImage(0, “CompressionCCITT4.tiff”);

opts.TiffCompression = TiffCompression.CompressionLZW;
sr = new SheetRender(ws, opts);
sr.ToImage(0, “CompressionLZW.tiff”);

opts.TiffCompression = TiffCompression.CompressionRle;
sr = new SheetRender(ws, opts);
sr.ToImage(0, “CompressionRle.tiff”);

Hi,


I’m afraid I’m still getting the same problem with the new dll you sent. Please find attached a console application that converts an excel sheet to multi-page tiff image. Change line 81 to change the compression type.

I wonder if this issue only pertains to multi-page tiffs or I’m doing something wrong.

All my outputs are 320KB as you can see in the folder App_Data\OutFiles.

Best regards
Robert Badi

Hi,


Thanks for your posting and using Aspose.Cells.

Please note

WorkbookRender.ToImage(stream/filepath) can convert whole workbook to tiff.
SheetRender.ToTiff(stream/filepath) can convert whole sheet to tiff.
SheetRender.ToImage(pageIndex, stream/filepath) can convert one page to tiff.

If you want to generate tiff yourself, you need to add code like:

ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)System.Drawing.Imaging.EncoderValue.CompressionCCITT3);

and change

EncoderParameters ep = new EncoderParameters(1);

to

EncoderParameters ep = new EncoderParameters(2);

Please check the attached screenshot for your reference

The first suggestion is simplest and works well.

Thank you so much.

Robert Badi.