I need to insert an enhanced meta file image (.emf) to my worksheet. The image has to scale without losing resolution that's why I'm using emf format.
I've tried inserting the image from a memory stream and reading from a file, but somewhere during the process the emf is converted to jpg and no longer scales as the spreadhseet is viewed/printed in different sizes.
Note, when I use Interop.Excel the image inserts as an emf file and scales appropriately. It's when I use EPPlus that I have the problem. I really want to get away from Interop.Excel thought, that's why I'm using EPPlus.
// This works as expected, but I don't want to use the interop lib.
using Excel = Microsoft.Office.Interop.Excel;
myXlWorkBook.ActiveSheet.shapes.AddPicture(emfImageFileName, ....);
// This does not. The picture appears in the worksheet but does not scale.
using OfficeOpenXml;
using OfficeOpenXml.Drawing;
ExcelPicture pic = myXlWorkSheet.Drawings.AddPicture("Picture Name", new FileInfo(emfImageFileName));
// This doesn't work either.
Image img = Image.FromStream(myEmfImageStream);
ExcelPicture pic = myXlWorkSheet.Drawings.AddPicture("Picture Name", img);
My expected result should be an Excel worksheet with my image in it that scales properly when viewed/printed in various sizes.
Make sure you give it dimensions otherwise everything will be zero:
pic.From.Row = 0;
pic.From.Column = 0;
pic.To.Row = 30;
pic.To.Column = 23;
As for storing as a JPG, that sounds very strange, it should not randomly convert. You can confirm if it is or not by opening the XLSX file in 7-zip or rename to .ZIP and open. You should see the file stored in the media folder.
Also, watch for spaces in the emf file name as I have seen that trip up EPPlus.
For anyone who might come across this, it is a bug with Epplus. I have submitted a PR"