-
Notifications
You must be signed in to change notification settings - Fork 276
Pictures
EPPlus supports adding/modifying/removing pictures on worksheets. Pictures are accessed via ExcelWorksheet
's Drawings
property and you can add them via a file path, a FileInfo
instance or a Stream
. You can also add a hyperlink to a picture by supplying an Uri
or an ExcelHyperlink
to the AddPicture
method.
Pictures are added to worksheets via the AddPicture
method, which is accessed via the Drawings
property on ExcelWorksheet
. This method has many overloads and returns an ExcelPicture
instance. Via the properties of ExcelPicture
you can set properties like size, border, fill and other effects.
The ExcelPicture
class also has an Image
property that represents the actual image. See the ExcelImage class below.
There are async versions of AddPicture
, these are named AddPictureAsync
.
Adding a picture/image to the workbook will work in a very similar way as adding a Shape
//Add a jpg image and apply some effects (EPPlus 6+ interface).
var pic = worksheet.Drawings.AddPicture("Landscape", new FileInfo("c:\\temp\\LandscapeView.jpg"));
pic.SetPosition(2, 0, 1, 0);
pic.Effect.SetPresetShadow(ePresetExcelShadowType.OuterBottomRight);
pic.Effect.OuterShadow.Distance = 10;
pic.Effect.SetPresetSoftEdges(ePresetExcelSoftEdgesType.SoftEdge5Pt);
var pics = worksheet.Drawings.Where(x => x.DrawingType == eDrawingType.Picture).Select(x => x.As.Picture);
foreach(var pic in pics)
{
var name = pic.Name;
var imageType = pic.Image.Type;
var width = pic.Image.Bounds.Width;
// etc
}
As with any other Drawing
you can use the SendToBack
and BringToFront
methods to adjust how a Picture is located vertically compared with other Drawings
.
pic.BringToFront();
// or
pic.SendToBack();
You can remove a shape from a worksheet via the Drawings.Remove
method.
// remove by instance
var itemToRemove = worksheet.Drawings.FirstOrDefault(x => x.Name == "myPic");
if(itemToRemove != null)
{
worksheet.Drawings.Remove(itemToRemove);
}
// remove by index
worksheet.Drawings.Remove(0);
// remove by name
worksheet.Drawings.Remove("myPic");
From EPPlus 6, the ExcelImage
class replaces all System.Drawing.Common.Image
properties (see list above).
Name | Description |
---|---|
ExcelImage() |
Creates an ExcelImage to be used as template for adding images. Use the SetImage methods to load the image after initialization |
ExcelImage(string imagePath) |
Creates an ExcelImage and loads an image from file. |
ExcelImage(FileInfo imageFile) |
Creates an ExcelImage and loads an image from file. |
ExcelImage(Stream imageStream, ePictureType pictureType) |
Creates an ExcelImage from a stream. |
ExcelImage(byte[] imageBytes, ePictureType pictureType) |
Creates an ExcelImage from a byte array. |
These properties are all readonly.
Name | Data type | Description |
---|---|---|
ImageBytes |
byte[] |
The image as a byte-array |
Type |
ePictureType |
The type of image, for example jpg, gif or svg |
Bounds |
ExcelImageInfo |
The bounds and resolution of the image |
Name | Description |
---|---|
SetImage(string) |
Sets the image from a file path |
SetImage(FileInfo) |
Sets the image from a FileInfo object |
SetImage(byte[], ePictureType) |
Sets the image from a byte array |
SetImage(Stream, ePictureType) |
Sets the image from a stream |
SetImage(ExcelImage) |
Sets the image from another image object |
SetImageAsync(string) |
Sets the image from a file path |
SetImageAsync(FileInfo) |
Sets the image from a FileInfo object |
SetImageAsync(Stream, ePictureType) |
Sets the image from a stream |
EPPlus Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles