Skip to content

Pictures

Mats Alm edited this page Dec 7, 2023 · 18 revisions

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.

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.

Example

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);

Landscape

Read existing pictures

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
}

Re-arrange vertically

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();

Remove a Picture

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");

The ExcelImage class

From EPPlus 6, the ExcelImage class replaces all System.Drawing.Common.Image properties (see list above).

Constructors

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.

Properties

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

Methods

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

See also

Sample 5.1 (C#) or Sample 5.1 (VB)

EPPlus wiki

Versions

Worksheet & Ranges

Styling

Import/Export data

Formulas and filters

Charts & Drawing objects

Tables & Pivot Tables

VBA & Protection

Clone this wiki locally