使用 EPPlus 在 GDI+ Drawings.AddPicture 中发生一般性错误

A generic error occurred in GDI+ Drawings.AddPicture using EPPlus

我想在Excel中添加图片。我使用图书馆 EPPLus。需要获取一张BASE64格式的图片,插入到Excel

中的单元格中

我的代码:

using (System.Drawing.Image img = Base64ToImage(base64String))
                            {
                             ExcelPicture excelImage = null;
                                if (img != null)
                                {
              ERROR HERE         excelImage = worksheet.Drawings.AddPicture("Title", img);
                                 excelImage.From.Column = 1;
                                 excelImage.From.Row = 1;
                                 excelImage.SetSize(100, 100);
                                }
                            }


public Image Base64ToImage(string base64String)
    {
        // Convert base 64 string to byte[]
        byte[] imageBytes = Convert.FromBase64String(base64String);
        // Convert byte[] to Image
        using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
        {
            Image image = Image.FromStream(ms, true);
            return image;
        }
    }

来自MSDN

You must keep the stream open for the lifetime of the Image.

我认为您不需要将 MemoryStream 包含在 using