Free Support Forum - aspose.com

Failing to convert to image


#1

I have an app that converts the first sheet of a spreadsheet to an image. The problem is that it bombs when doing the sr.ToImage function. Here is the error:

System.ArgumentException occurred
HResult=-2147024809
Message=Parameter is not valid.
Source=System.Drawing
StackTrace:
at System.Drawing.Drawing2D.GraphicsPath.get_PathPoints()
InnerException:

Here is the code:

Dim FileFormat As New Aspose.Cells.FileFormatInfo
Dim book As Aspose.Cells.Workbook
Select Case UCase(Extension)
Case “XLS”
book = New Aspose.Cells.Workbook(DocFile, New Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.Auto))
Case “XLSX”, “XLSB”, “XLSM”, “XLTX”, “XLTM”
book = New Aspose.Cells.Workbook(DocFile, New Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.Auto))
Case “CSV”
book = New Aspose.Cells.Workbook(DocFile, New Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.CSV))
End Select

Dim sheet As Aspose.Cells.Worksheet
sheet = book.Worksheets(0)

Dim imgOptions As New Aspose.Cells.Rendering.ImageOrPrintOptions()
'Specify the image format
imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png
'Only one page for the whole sheet would be rendered
imgOptions.OnePagePerSheet = True

'Render the sheet with respect to specified image/print options
Dim sr As Aspose.Cells.Rendering.SheetRender
Try
sr = New Aspose.Cells.Rendering.SheetRender(sheet, imgOptions)
sr.ToImage(0, “C:\Temp” & DocumentVersionID & “.1.png”)
Catch ex As Exception
sheet.Dispose()
book.Dispose()
ErrMsg = "Failed to open the render the sheet into png - " & ex.Message
GoTo nextfile
End Try

The problem is that it create the file, but keeps it open. If this bombs, I create my own image saying that the file was unable to be converted. The problem is that the file is being created, but not closed. So when I try and create the png, it says that it already exists and is open. I can’t find out how to close the file. I was assuming the sheet.dispose or book.dispose would do this. Can you let me know what to do here?

Here is the spreadsheet:

XLS.zip (312.0 KB)


#2

@runrobert,

Thanks for the template file and sample code.

I tested your scenario/ case using the following simplest code with your template file using our latest version/fix; Aspose.Cells for .NET v19.4.5 (please try it):
Aspose.Cells19.4.5 For .Net2_AuthenticodeSigned.Zip (4.9 MB)
Aspose.Cells19.4.5 For .Net4.0.Zip (4.9 MB)

it works fine and the output image (attached) is ok. I do not find any exception:
e.g
Sample code:

[C#]

string DocFile = "e:\\test2\\XLS.xls";

            Workbook book;
                    book = new Aspose.Cells.Workbook(DocFile, new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.Auto));
                
            Aspose.Cells.Worksheet sheet;
            sheet = book.Worksheets[0];
            
            Aspose.Cells.Rendering.ImageOrPrintOptions imgOptions = new Aspose.Cells.Rendering.ImageOrPrintOptions();
            
            //Specify the image format
            imgOptions.ImageFormat = System.Drawing.Imaging.ImageFormat.Png;
            //Only one page for the whole sheet would be rendered
           imgOptions.OnePagePerSheet = true;

           //Render the sheet with respect to specified image/print options
            Aspose.Cells.Rendering.SheetRender sr;

            try
            {

            sr = new Aspose.Cells.Rendering.SheetRender(sheet, imgOptions);
            sr.ToImage(0, "e:\\test2\\" + "out1.png");
            }
            catch (Exception ex)
            {
       
            sheet.Dispose();
            book.Dispose();
                string ErrMsg = "Failed to open the render the sheet into png - " + ex.Message;

                Console.WriteLine(ErrMsg);
            } 

Let us know if you still find the issue with v19.4.5.
out1.png (64.6 KB)