Creating tiff image with embedded icon from xlsx

We are using the Aspose,Cells 17.3. We are trying to convert xls files to tiff images. It works except when the xlsx has an embedded icon. The icon is not getting output in the tiff images. Here is the code we are using:


private void Foo()
{
Workbook book = new Workbook(“c:\temp\imagewithicon.xlsx”);
//Define ImageOrPrintOptions
var options = new ImageOrPrintOptions
{
SaveFormat = SaveFormat.TIFF,
HorizontalResolution = 200,
VerticalResolution = 200,
TiffCompression = TiffCompression.CompressionCCITT4
};

var i = 0;
foreach (var sheet in book.Worksheets)
{
//Render the sheet with respect to specified image/print options
var sr = new SheetRender(sheet, options);
//Render the image for the sheet
using (var imageStream = new MemoryStream())
{
sr.ToImage(i, imageStream);
imageStream.Seek(0, SeekOrigin.Begin);
var img = System.Drawing.Image.FromStream(imageStream);
img.Save($“D:\temp\Excel Converter Image\SheetImage{i}.tif”, ImageFormat.Tiff);
i++;
}
}
}

Please let me know what I need to do to get embedded images rendered. Thanks in advance.

Tim

Hi,


Thanks for your posting and using Aspose.Cells.

Please check the sampleExcel.xlsx. It contains the Word document as OLE object. The word document is shown as Icon.

We tested your sample code with the latest version and did not find any issue. Please check SheetImage0.tif. As you can see, the icon is rendered inside the image correctly.

Please first try the latest version and if you still find an issue, then provide us your source Excel file (i.e. imagewithicon.xlsx used inside your code) so that we could replicate the issue at our end and fix it.

Latest Version Links:
Aspose.Cells for .NET v17.4.6 (.NET 2.0) compiled in .NET Framework 2.0.
Aspose.Cells for .NET v17.4.6 (.NET 4.0) compiled in .NET Framework 4.0.

I have attached an example xls file that does not render the embedded image using the test code.


Tim

Hi,


Thanks for the template file.

I have evaluated your scenario/case a bit using your sample file and sample code. I think it looks to me Tiff compression issue/limit which does allow to render images or pictures in the sheet. I think you need to choose correct Tiff compression type to render images correctly.

Please change the line of code (in bold) from your code segment:
i.e.,
Sample code:


//Define ImageOrPrintOptions
var options = new ImageOrPrintOptions
{
SaveFormat = SaveFormat.TIFF,
HorizontalResolution = 200,
VerticalResolution = 200,
TiffCompression = TiffCompression.CompressionCCITT4
};

to:

//Define ImageOrPrintOptions
var options = new ImageOrPrintOptions
{
SaveFormat = SaveFormat.TIFF,
HorizontalResolution = 200,
VerticalResolution = 200,
TiffCompression = TiffCompression.CompressionLZW
};

Thank you.

For our purposes we must generate CCITT4 tif output. Why does CCITT4 compression not work?


Tim

Hi,


Thanks for your posting and using Aspose.Cells.

CompressionCCITT4 is not a good fit for you. Please check the following code. It does not use any Aspose.Cells API. We have generated two Tiff outputs. One is with CompressionCCITT4 and the other is with CompressionLZW.

As you can see CompressionLZW is OK but CompressionCCITT4 is not generating anything or any good output.

According to

MSDN - TiffCompressOption Enumeration

It seems CompressionCCITT4 is something like BlackAndWhite.

C#
FileStream fs = new FileStream(“testTiff.tiff”, FileMode.Create);

System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;
System.Drawing.Imaging.EncoderParameters ep = new System.Drawing.Imaging.EncoderParameters(2);
ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(enc, (long)System.Drawing.Imaging.EncoderValue.MultiFrame);
long compression = 0;
//compression = (long)System.Drawing.Imaging.EncoderValue.CompressionLZW;
compression = (long)System.Drawing.Imaging.EncoderValue.CompressionCCITT4;

ep.Param[1] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)compression);
System.Drawing.Imaging.ImageCodecInfo info = null;
System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
for (int i = 0; i < arrayICI.Length - 1; i++)
{
if (arrayICI[i].FormatDescription.ToUpper().Equals(“TIFF”))
{
info = arrayICI[i];
}
}


using (Image destImage = new Bitmap(200, 200))
{
using (Graphics g = Graphics.FromImage(destImage))
{
g.Clear(Color.White);
using (Image image = Image.FromFile(@“image1.png”))
{
g.DrawImage(image, 0, 50);
}
}
destImage.Save(fs, info, ep);
}

ep.Dispose();


We need CompressionCCITT4. In fact I can show that other color images work and are rendered. In fact this use to work in version 14.2. Would you like me to send you an example?


Tim

Hi,


Thanks for your feedback and using Aspose.Cells.

Yes, please send us a console (.NET C#) application project that should be runnable and not complex (it should be a simple project) for our testing. We will execute it and check it and log the ticket for your issue so that it could be fixed in our future releases. Thanks for your cooperation in this regard and have a good day.

Ok I am back from vacation. See two attachments. The xlsx with another image icon and the output image from the code I listed above. You will see a light image is output even though the image is color using CCITT4 compression.



Tim

Hi,


Thanks for your posting and using Aspose.Cells.

We have looked into this issue further and found that the image you have shared can also be created with the code we shared previously.

Please try our code (which does not use Aspose.Cells at all) in this post i.e. 841218.

It is actually the brightness of pixels in the source image that affects the output in CompressionCCITT4 tiff.

Again we cant use your suggested compression method based on our business needs. Can you please explain why some images come out using <span style=“color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; font-size: 15px; background-color: rgb(255, 255, 255);”>CCITT Group 4 and others do not? This seems like a bug!

<span style=“color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; font-size: 15px; background-color: rgb(255, 255, 255);”>
<span style=“color: rgb(36, 39, 41); font-family: Arial, “Helvetica Neue”, Helvetica, sans-serif; font-size: 15px; background-color: rgb(255, 255, 255);”>Tim

Hi,


Thanks for your posting and using Aspose.Cells.

We have logged your issue in our database for further investigation. We will look into it and help you out. Once, there is some update for you, we will let you know asap.

This issue has been logged as

  • CELLSNET-45404 - Creating Tiff image with embedded icon from Xlsx

Hi,

Thanks for using Aspose.Cells.

First, a tiff with CompressionCCITT4 is a BlackAndWhite image. When a colorful image (e.g. Format24bppRgb) is converted to a BlackAndWhite (Format1bppIndexed) image, the pixels need to be converted by a rule (e.g. if the brightness of a pixel in colorful image larger than a threshold, then the pixel will be white in BlackAndWhite image, else the pixel will be black in BlackAndWhite image). This is why some images come out and others do not.

In our attachment, only text color in image "test_Yellow.png" and "test_Black.png" is different. You will find that text in "test_Yellow.png" will be white in CompressionCCITT4 tiff (you will see nothing in it), and text in "test_Black.png" will be black in CompressionCCITT4 tiff.

For your image not coming out of file, you need to replace the image with a darker image.