Adding Images to a Worksheet with ExpertXls |
Add pictures to the Pictures collection of a worksheet with the AddPicture method. The image is a System.Drawing.Image object or the path of an image file. The position is given by the top left cell (one based column and row indexes); the size is the real size of the image, a scale, or the range of cells the picture covers.
A System.Drawing.Image object must stay alive until the workbook is saved; dispose it after the save.
using (Image demoImage = Image.FromFile(Path.Combine(imagesFolder, "demo_250x300.jpg"))) { // at real size, with the top left corner in column 1, row 7 (A7) ExcelPicture picture = worksheet.Pictures.AddPicture(1, 7, demoImage); // scaled to 75% of the width and height worksheet.Pictures.AddPicture(1, picture.BottomRowIndex + 2, demoImage, 75, 75); // stretched over the range A40:E60 (the aspect ratio is not kept) worksheet.Pictures.AddPicture(1, 40, 5, 60, demoImage); // loaded by the library from a file ExcelPicture logo = worksheet.Pictures.AddPicture(8, 7, Path.Combine(imagesFolder, "logo.jpg")); // the picture moves with the cells but keeps its size logo.PicturePlacement = ExcelPicturePlacement.MoveDontSizeWithCells; // the cells covered by the first picture string area = String.Format("columns {0} to {1}, rows {2} to {3}", picture.LeftColumnIndex, picture.RightColumnIndex, picture.TopRowIndex, picture.BottomRowIndex); workbook.Save("Images.xlsx"); }
A picture can also be shown in the header or footer of the printed page; see Worksheet Page Setup.