Home » excel » c# – EPPlus Excel error when using AddPicture

c# – EPPlus Excel error when using AddPicture

Posted by: admin May 14, 2020 Leave a comment


I been trying to find the solution for this for so many hours i’m really tired, i hope someone can indicate me with i’m missing. I will try to be as clear as posible with all the possible information so there is no confusion.

I’m using EPPlus, Visual Studio 2010, C#, MVC.NET Framework 4.0 and MS Excel 2007

I’m just trying to do a simple thing: download an Excel file with a picture in it. Nothing else.

I have an action that opens an excel file, fills it with data and adds a picture which code looks like this:

public ActionResult FillExcelFile(string imagePath)
    FileInfo template = new FileInfo([path_of_excel_file]);

    ExcelPackage xls = new ExcelPackage(template);
    ExcelWorksheet worksheet = xls.Workbook.Worksheets["Sheet1"];

    worksheet.Cells[1, 1].Value = "data1";
    worksheet.Cells[1, 2].Value = "data2";
    worksheet.Cells[1, 3].Value = "data3";

    /* ToDo: add picture */

    return File(xls.GetAsByteArray(), "application/vnd.ms-excel", "excel.xlsx"));

At this moment everything works great! The file have the information and i can download and open it with no problem at all.

Now i will add the picture i want, i will change the ToDo part with the next code:

Image img = Image.FromFile(imagePath);
ExcelPicture pic = worksheet.Drawings.AddPicture("img", img);
pic.SetPosition(1, 1);

Run it, download it, open it aaand… error:

Excel found unreadable content in “excel.xlsx”. Do you want to recover… blah blah blah

Of course i want to recover.

Files open aaand… is empty and an error appear:

Replaced Part: /xl/worksheets/sheet1.xml part with XML error. Load error. Line…

From this moment till now i been adding different code:

Image img = Image.FromFile(imagePath);
ExcelPicture pic = worksheet.Drawings.AddPicture("img", img);
pic.SetPosition(1, 1);
pic.SetSize(100, 100);


Bitmap img = new Bitmap(Image.FromFile(imagePath));
ExcelPicture pic = worksheet.Drawings.AddPicture("img", img);
pic.SetPosition(1, 1);


FileInfo img = new FileInfo(imagePath);
ExcelPicture pic = worksheet.Drawings.AddPicture("img", img);
pic.SetPosition(1, 1);

With the last one there as exception:

: System.ArgumentException: Part URI is not valid per rules defined in the Open Packaging Conventions specification

And many many many more like open the image with a stream, adding the setSize because someone said some kind of problem if you don’t define it, define my own URI, save the file in the server then download it, etc.

I really appreciate any help that you can give me, really. I don’t known what else to check. If you require more information be free to ask.

How to&Answers:

i found the problem… or kind of. Thanks to @Chris to guide me in this one and to @Ernie for the sugestion.

The problem is the template file that i was trying to fill has something inside that doesn’t work fine when i try to add an image. @Chris says “you already have a drawing part in the template and EPPlus is creating some sort of conflict”. I don’t know what could that be.

So i create a new template from scratch (the template i was using was done by someone else) and everything worked like charm.

Steps that i performed:

  • I created a new file with using EPPlus and download it and it worked.
  • I created a new empty template and add the image with EPPlus and it worked.
  • I created a new template with all the stuff i needed in it and it worked.

I hope this help someone else if they have a similar problem.


I’ve had the same issue and noticed that when you add a background-picture before adding the picture the file gets corrupted. If you add the background picture afterwards it works fine.

osheet.BackgroundImage.Image = My.Resources.anyimage


I had the same problem. With prueba prueba’s answer I analysed my original template and found this: If the table formatting feature (see screenshot) is used then AddPicture causes the XML errors. My solution was to remove the table formatting.

table formatting feature


I found the same problem. I’m using the version

I try to use this code:

Bitmap arquivoLogotipo = new Bitmap(@"C:\xxxxxx.jpg");
            ExcelPicture logotipoExcel = principal.Drawings.AddPicture("logotipo", arquivoLogotipo);
            logotipoExcel.SetPosition(50, 250);

I try this too:

FileInfo logotipoEndereco = new FileInfo("C\okokoko.jpg");
var logotipo = principal.Drawings.AddPicture("logotipo", logotipoEndereco);
                logotipo.SetPosition(50, 250);

In my case, I just have a blank sheet. My cod just insert a name of a project and one image.I really don’t know what is happening.

Thanks :D!