Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu May 27, 2021 10:33 am

Hi,

I tried the FreeSpire.XLS component for converting an excel file (xlsx format) into tiff file. This works pretty easy with FreeSpire.XLS component. But i noticed differences on each system, I tried to convert the sample file into the output format tiff.

On dev environment (see attachment output_dev.tif), there is a lots of space, but I´m using worksheet.LastRow / worksheet.LastColumn properties to identify the end of sheet. Also when I´m entering manual the last row / last column, the space is generated in the output file.

On test environment, the problem with the space does not occure, but as you can see on files output_test_new.tif and output_test_old.tif there are differences in the output file and also differences at the resolution:

output_prod_new.tif has 4404x3402 pixels while
output_prod_old.tif has 5871 x 4542 pixels

Is there any way to define one standard, the files has to be generated? (e.g. setting a fixed font or system properties)

Code for converting the files:

Code: Select all
 public class ExcelToTiffTransformator
    {
        #region Fields & Properties
        /// <summary>
        /// Mime type for find of image codec.
        /// </summary>
        private const string MIME_TYPE = "image/tiff";
       
        /// <summary>
        /// Output resolution for Tiff file.
        /// </summary>
        private const int tiffOutputDPI = 400;
       
        /// <summary>
        /// Tiff compression type.
        /// </summary>
        private EncoderValue TiffCompression
        {
            get
            {
                return EncoderValue.CompressionCCITT4;
            }
        }
        #endregion

        #region Public methods
        /// <summary>
        /// Converts the specified excel file to tiff output.
        /// </summary>
        /// <param name="excelFileToConvert">Excel to convert first worksheet.</param>
        /// <param name="tiffOutputPath">Path of result tiff file.</param>
        public void ConvertToTiff(string excelFileToConvert, string tiffOutputPath, int lastRow = -1, int lastColumn = -1)
        {
            Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
            workbook.LoadFromFile(excelFileToConvert);

            Spire.Xls.Worksheet worksheet = workbook.Worksheets[0];
           
            using (MemoryStream stream = new MemoryStream())
            {
                int paramFirstRow = 0;
                int paramFirstColumn = 0;
                int paramLastRow = lastRow != -1 ? lastRow : worksheet.LastRow;
                int paramLastColumn = lastColumn != -1 ? lastColumn : worksheet.LastColumn;

                // Get image and vector informations from worksheet for worksheet content.
                worksheet.ToEMFStream(stream, paramFirstRow, paramFirstColumn, paramLastRow, paramLastColumn);

                Image imageFile = Image.FromStream(stream);

                // Resize image to the desired quality.
                Bitmap images = ResetResolution(imageFile as Metafile, tiffOutputDPI);

                // Encoder parameters for CCITT 6
                EncoderParameters encoderParameters = new EncoderParameters(1);
                encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)TiffCompression);

                ImageCodecInfo tiffEncoder = GetImageCodec();

                if (tiffEncoder == null)
                {
                    throw new NotSupportedException("No image codec found!");
                }
               
                // Save image to specified location with specific codec settings.
                images.Save(tiffOutputPath, tiffEncoder, encoderParameters);
            }
        }
        #endregion

        /// <summary>
        /// Find image codec for save as tiff file.
        /// </summary>
        /// <returns>
        /// <see cref="ImageCodecInfo"/> for tiff file.
        /// </returns>
        private ImageCodecInfo GetImageCodec()
        {
            if (ImageCodecInfo.GetImageEncoders() == null) return null;
            if (!ImageCodecInfo.GetImageEncoders().ToList().Any(i => i.MimeType == MIME_TYPE)) return null;

            return ImageCodecInfo.GetImageEncoders().ToList().Find(i => i.MimeType == MIME_TYPE);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="metaFile">Meta file, containing vector informations for resing.</param>
        /// <param name="resolution">Resolution to resize the graphic to</param>
        /// <returns>
        /// Image, recalculated by metafile for desired resultion.
        /// </returns>
        private Bitmap ResetResolution(Metafile metaFile, float resolution)
        {
            // Calulate new width and height according to resolution and existing resolution.
            int width = (int)(metaFile.Width * resolution / metaFile.HorizontalResolution);
            int height = (int)(metaFile.Height * resolution / metaFile.VerticalResolution);
           
            // Create new bitmap with new dimensions.
            Bitmap bitmap = new Bitmap(width, height);
            bitmap.SetResolution(resolution, resolution);

            // Draw the new bitmap in calculated dimensions.
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.DrawImage(metaFile, 0, 0);
            }
           
            return bitmap;
        }
    }


Thanks in advance for your help

Best regards
Alexander Kommer
Attachments
output_test_old.tif
output test old
output_test_old.tif (9.64 KiB) Viewed 1955 times
output_test_new.tif
output test new
output_test_new.tif (3.24 KiB) Viewed 1955 times
output_dev.tif
output dev
output_dev.tif (2.85 KiB) Viewed 1955 times

Alexande.Kommer
 
Posts: 3
Joined: Thu May 27, 2021 9:18 am

Fri May 28, 2021 5:53 am

Hello,

Thanks for your inquiry.
To help us investigate your issue more quickly and accurately, please provide the following information.
1) Your test .xlsx file.
2) Details of your test environment and dev environment, including OS information (e.g. Windows7, 64bit) and region settings (e.g. China, Chinese) and DPI settings.
3) Regarding the two files “output_test_new.tif” and “output_test_old.tif”, are you generating them in the exact same environment using the same input file and code?

You could send them to us (support@e-iceblue.com) via email. Thanks in advance.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Wed Jun 02, 2021 12:35 pm

Hello,

Thanks for your sharing via email.
When testing on Windows 10 and the DPI is set to 125%, I did notice that the converted image was not correct. I have logged it in our bug tracking system with the ticket SPIREXLS-3278. If there is any update, we will let you know.

As for the issue of different image dimensions when converting Excel to tif under different systems, I tested the following code (without resetting resolution) on several different systems and found that the image dimensions are all the same (1057*817). However, after the resetting the resolution, the image dimensions are different. The ResetResolution method you use is actually an API provided by C#, which does not use our Spire.PDF. This issue is not caused by our product, hope you can understand.
Code: Select all
            Spire.Xls.Workbook workbook = new Spire.Xls.Workbook();
            workbook.LoadFromFile(excelFileToConvert);
            Spire.Xls.Worksheet worksheet = workbook.Worksheets[0];
            using (MemoryStream stream = new MemoryStream())
            {
                int paramFirstRow = 0;
                int paramFirstColumn = 0;
                int paramLastRow = worksheet.LastRow;
                int paramLastColumn = worksheet.LastColumn;
                worksheet.ToEMFStream(stream, paramFirstRow, paramFirstColumn, paramLastRow, paramLastColumn);
                Image imageFile = Image.FromStream(stream);
                imageFile.Save("result.png", ImageFormat.Png);
            }


Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Mon Sep 26, 2022 9:29 am

Hi,

Thanks for your patient waiting.
Glad to inform you that we just released Spire.XLS Pack(Hotfix) Version:12.9.2 which fixes your reported issue SPIREXLS-3278. Welcome to download the hotfix from the following link.
Website download link: https://www.e-iceblue.com/Download/download-excel-for-net-now.html
Nuget download link: https://www.nuget.org/packages/Spire.XLS/12.9.2

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1182
Joined: Tue Sep 27, 2016 1:06 am

Return to Spire.XLS