Loading excel file with images fails on SaveAs : System.Drawing.Image.Save - Value cannot be null - Parameter name: encoder
I'm using EPPlus.Core to load in a xlsm file, insert values into the file, then save it to a stream which is sent as an email attachment.
The process fails with the following error:
An unhandled exception occurred while processing the request. ArgumentNullException: Value cannot be null. Parameter name: encoder System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
I'm assuming that EPPlus is scanning the whole file, then saving everything to the new stream, but when it encounters an image it doesn't know/like the image format (jpg/png/?) and then doesn't know what encoder to use.
I'm only inserting data into the spreadsheet before saving, which will ultimately run several complicated macros on the recipients machine, so I don't want to have to build the entire spreadsheet from scratch (which would give me the control over image encoding).
Is there somewhere that the encoding can be set globally for the new stream?
Environment
EPPlus.Core (1.5.4) .Net Core 2.0
Example code/Steps to reproduce:
If images are stripped out the following works, otherwise fails at excel.SaveAs
public async Task Test()
{
string myFile = "ClientApp/src/assets/templates/test-workbook.xlsx";
var file = Path.Combine(Directory.GetCurrentDirectory(), myFile);
FileInfo template = new FileInfo(file);
MemoryStream outputStream = new MemoryStream();
using (var excel = new ExcelPackage(template))
{
var ws = excel.Workbook.Worksheets[1];
ws.Cells["B10"].Value = "Test";
excel.SaveAs(outputStream); // Fails here
outputStream.Position = 0;
// ... Code for creating & emailing attachment
}
}
Output:
ArgumentNullException: Value cannot be null. Parameter name: encoder System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) OfficeOpenXml.ImageConverter.ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) OfficeOpenXml.Drawing.ExcelPicture..ctor(ExcelDrawings drawings, XmlNode node) OfficeOpenXml.Drawing.ExcelDrawing.GetDrawing(ExcelDrawings drawings, XmlNode node) OfficeOpenXml.Drawing.ExcelDrawings.AddDrawings() OfficeOpenXml.ExcelWorksheet.Save() OfficeOpenXml.ExcelWorkbook.Save() OfficeOpenXml.ExcelPackage.Save() OfficeOpenXml.ExcelPackage.SaveAs(Stream OutputStream)