EPPlus worksheet.Drawings.AddPicture() 不支持 docker 上的 Jpg/Png

问题描述 投票:0回答:2

我目前正在编写一个程序,该程序生成条形码,然后将其本地保存为具有标准文件扩展名(例如 jpeg、png)的图像。之后,我需要使用 worksheet.Drawings.AddPicture() 方法将生成的条形码图像导入到新创建的 Excel 文件中。当我将程序作为控制台应用程序运行时,一切正常,但是当我使用 Docker 运行它时,会引发以下异常:

System.InvalidOperationException: No image handler for image type Jpg
   at OfficeOpenXml.Drawing.PictureStore.GetImageBounds(Byte[] image, ePictureType type, ExcelPackage pck)
   at OfficeOpenXml.Drawing.PictureStore.AddImage(Byte[] image, Uri uri, Nullable`1 pictureType)
   at OfficeOpenXml.Drawing.ExcelPicture.SaveImageToPackage(ePictureType type, Byte[] img)
   at OfficeOpenXml.Drawing.ExcelPicture.LoadImage(Stream stream, ePictureType type)
   at OfficeOpenXml.Drawing.ExcelDrawings.AddPicture(String Name, FileInfo ImageFile, Uri Hyperlink)
   at OfficeOpenXml.Drawing.ExcelDrawings.AddPicture(String Name, FileInfo ImageFile)

对于条形码生成,我使用 ZXing.Net 版本 0.16.8。 对于图像生成,我使用 ImageSharp 版本 2.1.3(因为据我所知,标准 System.Drawing.Common 库仅在 Windows 上受支持)

string temporaryBarcodeImagePath = $"barcode.jpeg";

var barcodeMatrix = _barcodeManager.GenerateCode128(order.ReferenceNumber, 400, 20);
await _barcodeManager.SaveBarcodeImageAsync(temporaryBarcodeImagePath, barcodeMatrix);
var picture = worksheet.Drawings.AddPicture("Barcode", temporaryBarcodeImagePath);

picture.SetPosition(20, 20);

_barcodeManager 只是一个在文件系统本地生成并保存条形码图像的服务。我不认为这个问题与此有关。

到目前为止我发现的唯一类似问题是以下 github 问题:https://github.com/EPPlusSoftware/EPPlus/issues/645

但是,问题中指出,有问题的错误已在 EPPlus 版本 6.0.5 中修复,这正是我当前使用的版本。

任何帮助将不胜感激。预先感谢!

c# excel docker asp.net-core epplus
2个回答
0
投票

我在以下 github 问题线程中提供了一些解释:

https://github.com/EPPlusSoftware/EPPlus/issues/645


0
投票

创建ExcelPackage后即可使用此配置;

excelPackage.Settings.ImageSettings.PrimaryImageHandler = new SystemDrawingImageHandler();

© www.soinside.com 2019 - 2024. All rights reserved.