Магжан Куан
Магжан Куан

Reputation: 964

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

I want to add a picture in Excel. I use the library EPPLus. It is necessary to obtain an image of the BASE64 format and inserted into the cell in Excel

My Code:

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;
        }
    }

Upvotes: 1

Views: 1568

Answers (1)

Denis Palnitsky
Denis Palnitsky

Reputation: 18387

From MSDN:

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

I don't think you need to enclose MemoryStream in using

Upvotes: 5

Related Questions