Search code examples
c#excelepplus

How can I programmatically set images to move and size with cells?


You can change how an image is affected by resizing columns and rows in Excel by right clicking the image, clicking "Size and Properties", and then selecting "Properties". By default, it is "Move but don't size with cells". I want to programmatically set images to "Move and size with cells" using EPPlus. If that's not possible, is it possible to change it to "Move and size with cells" for all images on the sheet?

Here's the code that inserts each image:

System.Drawing.Image thumbnail = System.Drawing.Image.FromFile(imagePath);                            
var picture = worksheet.Drawings.AddPicture(data, thumbnail);
picture.SetSize(65, 20);                            
picture.SetPosition(j - 1, 0, 3 + x++, 0);

Solution

  • Set the picture to TwoCell in the EditAs setting:

    picture.EditAs = eEditAs.TwoCell;
    

    This will tell excel to stretch it.