Skip to main content

IXlSheet.CreatePicture() Method

Creates a picture to be inserted into the worksheet.

Namespace: DevExpress.Export.Xl

Assembly: DevExpress.Printing.v24.1.Core.dll

NuGet Package: DevExpress.Printing.Core

Declaration

IXlPicture CreatePicture()

Returns

Type Description
IXlPicture

The inserted picture.

Remarks

Follow the steps below to add a picture to a worksheet:

  1. Call the CreatePicture method to create a new IXlPicture object.
  2. Call the IXlPicture.SetImage method to specify the image you wish to display in the worksheet. Use the Image.FromFile or Image.FromStream method to load the required image from a file or data stream.
  3. To position the picture in the worksheet, use one of the following methods depending on the anchor type you wish to use:

    Absolute Anchoring

    Use the IXlPicture.SetAbsoluteAnchor method to specify the absolute picture position in the worksheet. The position is defined by offsets from the worksheet’s left and top edges to the picture’s top-left corner. The picture does not move or resize with the underlying cells.

    One Cell Anchoring

    Use the IXlPicture.SetOneCellAnchor method to anchor the picture’s top-left corner to a worksheet cell. The picture moves with the anchor cell, but the picture size remains the same.

    Two Cell Anchoring

    Use the IXlPicture.SetTwoCellAnchor method to anchor the picture to two cells in the worksheet. The first anchor cell specifies the position of the picture’s top-left corner, and the second anchor cell defines the position of the picture’s bottom-right corner. The method’s last parameter specifies how the picture behaves when the underlying columns and rows are resized and moved (IXlPicture.AnchorBehavior).

Note

When you finish working with the IXlPicture object, call the Dispose method to release all the resources used by the object. Otherwise, generated content is not written to the output file. You can also modify the IXlPicture object within the using statement (Using block in Visual Basic).

Example

The example below uses the IXlPicture.SetTwoCellAnchor method to anchor a picture to two cells in a worksheet. The anchor for the picture’s top-left corner is located at the intersection of the column “B” and the second row, with no offsets. The anchor for the picture’s bottom-right corner is located at the intersection of the column “G” and the twelfth row, with offsets from both the column and row. The IXlPicture.AnchorBehavior is set to XlAnchorType.TwoCell.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/excel-export-api-examples

// Create a worksheet.
using (IXlSheet sheet = document.CreateSheet())
{

    // Insert a picture from a file and anchor it to cells. 
    using (IXlPicture picture = sheet.CreatePicture())
    {
        picture.SetImage(Image.FromFile(Path.Combine(imagesPath, "image1.jpg")), ImageFormat.Jpeg);
        picture.SetTwoCellAnchor(new XlAnchorPoint(1, 1, 0, 0), new XlAnchorPoint(6, 11, 2, 15), XlAnchorType.TwoCell);
    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the CreatePicture() method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also