IXlSheet.CreatePicture() Method

Creates a picture to be inserted into the worksheet.

Namespace: DevExpress.Export.Xl

Assembly: DevExpress.Printing.v21.2.Core.dll

Declaration

IXlPicture CreatePicture()

Returns

Type Description
IXlPicture

An IXlPicture object that is the inserted picture.

Remarks

To add a picture to a worksheet, do the following.

  1. Call the CreatePicture method to create a new IXlPicture object.
  2. Set the IXlPicture.Image property to the Image object that specifies the image to be displayed in the worksheet.
  3. To specify the picture’s position in the worksheet, use one of the following methods depending on the anchoring type you wish to use.

    • Absolute Anchoring

      Use the IXlPicture.SetAbsoluteAnchor method to specify the absolute position of the picture within the worksheet and set the picture’s width and height. The picture will not move or resize with the underlying cells.

    • One Cell Anchoring

      Use the IXlPicture.SetOneCellAnchor method to specify the start position for the picture and set the picture’s width and height. The picture will move with the anchor cell, but its size will remain the same.

    • Two Cell Anchoring

      Use the IXlPicture.SetTwoCellAnchor method to specify the start and end positions for the picture and define its anchoring behavior.

For details, refer to the How to: Insert and Position a Picture in a Worksheet article.

Example

The example below demonstrates how to use the IXlPicture.SetTwoCellAnchor method to anchor a picture to two cells in a worksheet. The starting anchor for a picture is located at the intersection of the second column (“B”) and the second row, with no offsets. The final anchor is located at the intersection of the seventh column (“G”) and the twelfth row, with offsets from both the column and row. The IXlPicture.AnchorBehavior is set to XlAnchorType.TwoCell, so that the picture moves and resizes with its underlying cells.

// 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.Image = Image.FromFile(Path.Combine(imagesPath, "image1.jpg"));
        picture.SetTwoCellAnchor(new XlAnchorPoint(1, 1, 0, 0), new XlAnchorPoint(6, 11, 2, 15), XlAnchorType.TwoCell);
    }
}
See Also