WorkBook Save Fails

I made the suggested changes and downloaded the new DLL.
Results:

[10/18/2017 13:59:34 > 02108d: SYS INFO] Status changed to Initializing
[10/18/2017 13:59:34 > 02108d: SYS INFO] Job directory change detected: Job file ‘CellsTest.exe’ timestamp differs between source and working directories.
[10/18/2017 13:59:35 > 02108d: SYS INFO] Run script ‘CellsTest.exe’ with script host - ‘WindowsScriptHost’
[10/18/2017 13:59:35 > 02108d: SYS INFO] Status changed to Running
[10/18/2017 13:59:38 > 02108d: INFO] Exception: Shape to image Error!
[10/18/2017 13:59:38 > 02108d: INFO] Exception: at Aspose.Cells.Workbook.Save(String fileName, SaveOptions saveOptions)
[10/18/2017 13:59:38 > 02108d: INFO] at Aspose.Cells.Workbook.Save(String fileName)
[10/18/2017 13:59:38 > 02108d: INFO] at CellsTest.Program.Main(String[] args)
[10/18/2017 13:59:38 > 02108d: SYS INFO] Status changed to Success

Interesting enough, the .png produced by the sample code was 4k when I ran in debug mode, only 151 bytes when I ran as web job.
Maybe that’s a clue.

Dave

@dwgrooms,

Thanks for sharing the results.

I have logged the results and findings against your issue “CELLSNET-45704” into our database. Our concerned developer from product team will evaluate your issue further soon.

@dwgrooms,

Please try executing the following lines of code on Azure and check that the file ‘Result.emf’ is correct:
e.g
Sample code:

    RectangleF dstRectF = new RectangleF(0, 0, 673f, 360f);
    Bitmap dummyBitmap = new Bitmap(1, 1);
    Graphics dummyGfx = Graphics.FromImage(dummyBitmap);
    hdc = dummyGfx.GetHdc();

    MemoryStream msOut = new MemoryStream();
    Metafile metafile = new Metafile(msOut, hdc, dstRectF, MetafileFrameUnit.Pixel, EmfType.EmfPlusDual);
    Graphics g = Graphics.FromImage(metafile);

    byte[] Data = SaveImage(path +"\\image1.emf");
    Stream Ms = new MemoryStream(Data);
    Image Ima = Image.FromStream(Ms,true);

    g.DrawImage(Ima, dstRectF);
    g.Dispose();

    msOut.Seek(0, SeekOrigin.Begin);
    Image img = Image.FromStream(msOut);
    img.Save(path + "\\Result.emf", ImageFormat.Emf); //or img.Save(path + "\\Result.png", ImageFormat.Png);

Note: ‘image1.emf’ is the picture in Excel.
If no error is occurred. Please provide two files that you generated in debug run and network job(4k\151bytes).

Can you provide me with a working example ?

The fragment is incomplete, I don’t have a method called SaveImage, so the fragment doesn’t even compile.

Dave

@dwgrooms,

We will check if we could provide you the exact lines of code written in “SaveImage” custom method, so you may compile the code segment seamlessly. But, the underlying method only gets the image file into byte array, so you may easily replace the following line with your own code to read the contents of the image file into array of bytes by yourself:

byte[] Data = SaveImage(path +"\\image1.emf");

In Azure
[10/23/2017 15:03:47 > 1c59f5: SYS INFO] Status changed to Initializing
[10/23/2017 15:03:47 > 1c59f5: SYS INFO] Job directory change detected: Job file ‘CellsTest.exe’ timestamp differs between source and working directories.
[10/23/2017 15:03:48 > 1c59f5: SYS INFO] Run script ‘CellsTest.exe’ with script host - ‘WindowsScriptHost’
[10/23/2017 15:03:48 > 1c59f5: SYS INFO] Status changed to Running
[10/23/2017 15:03:48 > 1c59f5: INFO] basepath :D:\local\Temp\jobs\triggered\CellsTest\4idojlqp.gir
[10/23/2017 15:03:51 > 1c59f5: INFO] OutPath :D:\local\Temp\jobs\triggered\CellsTest\4idojlqp.gir\data\Sheet1Shape[0].Object 1.png
[10/23/2017 15:03:51 > 1c59f5: INFO] Exception: A generic error occurred in GDI+.
[10/23/2017 15:03:51 > 1c59f5: INFO] Exception: at System.Drawing.Imaging.Metafile…ctor(Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type, String description)
[10/23/2017 15:03:51 > 1c59f5: INFO] at System.Drawing.Imaging.Metafile…ctor(Stream stream, IntPtr referenceHdc, RectangleF frameRect, MetafileFrameUnit frameUnit, EmfType type)
[10/23/2017 15:03:51 > 1c59f5: INFO] at CellsTest.Program.Main(String[] args)
[10/23/2017 15:03:51 > 1c59f5: SYS INFO] Status changed to Success

After a bit of research, it looks like the GDI+ calls for MetaFile usage are not supported in an Azure WebJob.

Is there any way to accomplish the image transfer without these APIs ?

Otherwise I may have to rethink my hosting of this service

Thanks,
Dave

@dwgrooms,

That might be the reason it fails for image/shape rendering. I have logged your log contents and findings against your issue “CELLSNET-45704” into our database. Our concerned developer from product team will evaluate it further and we will get back to you soon.

@dwgrooms,

Well, Metafile(e.g. emf, wmf) images are widely used in Microsoft Excel. A solution is that we can add an option to let you set to draw metafile to bitmap, but this may cause the output image a little blurred in the output PDF.

Could you try to check whether the following code works on your Azure web job:
e.g
Sample code:

using (Image srcImage = Image.FromFile("image1.emf"))
            {
                using (Bitmap destImage = new Bitmap(srcImage.Width, srcImage.Height))
                {
                    destImage.SetResolution(srcImage.HorizontalResolution, srcImage.VerticalResolution);
                    using (Graphics g = Graphics.FromImage(destImage))
                    {
                        g.DrawImage(srcImage, new RectangleF(0, 0, srcImage.Width, srcImage.Height));
                    }

                    using (MemoryStream ms = new MemoryStream())
                    {
                        destImage.Save(ms, ImageFormat.Png);

                        //test save to file
                        using (FileStream fs = new FileStream("imageOut.png", FileMode.Create))
                        {
                            byte[] imageData = ms.ToArray();
                            fs.Write(imageData, 0, imageData.Length);
                        }
                        
                    }
                }
            }

if the code works and you accept the solution, we will add an option for you.

Please note, “image1.emf” is extracted from your Excel file.
image1.zip (21.1 KB)

No luck, the Image.FromFile throws and exception of unsupported file format in Azure, works fine from console.

I’m going to open a ticket with Microsoft and see if they have an answer, otherwise my only option is to re-architecht the application.

Thanks,
Dave

@dwgrooms,

We are sorry that this does not work in Azure. As you are also approaching Microsoft to sort it out (if possible). Your thread (logged earlier as "CELLSNET-45704 ") is till open and we will check and evaluate further if we have other options to cope with it.

Once we have any new information, we will update you here.

Thanks,
What would be the possibility of an option flag that skipped the images and converted the rest of the file ?

Dave

@dwgrooms,

I think you may try to remove the image(s) in the file (in code) before rendering, see the sample line of code:
e.g
Sample code:


worksheet.Pictures.RemoveAt(index);

Alternatively, could you try using filtering option while loading your file in Azure. See the sample code segment for your reference:
e.g
Sample code:

 var filePath = "e:\\test2\\Book1.xlsx";
//Load everything except shapes.
            var loadFilter = new LoadFilter(LoadDataFilterOptions.All & ~LoadDataFilterOptions.Shape);
            var asposeOptions = new Aspose.Cells.LoadOptions
            {
                MemorySetting = MemorySetting.MemoryPreference,
                LoadFilter =  loadFilter
            };
            var workbook = new Workbook(filePath, asposeOptions);

About the time I sent the message to ask for the flag I realized I could just remove the shapes myself.
I did so and that has got me past the issue.

Thanks for all the assistance,

Dave

@dwgrooms,

Good to know that your issue is sorted out now. Feel free to write us back if you have further comments or questions, we will be happy to assist you soon.

@dwgrooms,

We can add an option to ignore error while rendering shapes/images, that will render as much shapes/images as possible instead of removing them.

Do you want us to add this feature?

That does sound like a useful option.
Let me know when you have a version with this and I can tr it out.

Thanks,
Dave

@dwgrooms,

Thanks for your confirmation.

Ok, we will try to add the option/feature soon.

Once we implement it or we have some other update on it, we will let you know here.

@dwgrooms,

Please try our latest fix/version: Aspose.Cells for .NET v17.10.3:

We added API: PdfSaveOptions.IgnoreError in the fix:
e.g
Sample code:

        Workbook wb = new Workbook(srcFile); 

        PdfSaveOptions pdfSaveOpt = new PdfSaveOptions(); 
        pdfSaveOpt.IgnoreError = true; 

        wb.Save(outFile.pdf, pdfSaveOpt);

Let us know your feedback.