Skip to main content
You are viewing help content for pre-release software. This document and the features it describes are subject to change.
All docs
V26.2
  • Insert and Manage Pictures

    • 9 minutes to read

    Spreadsheet Document API allows you to insert in-cell and floating images into a worksheet. Resize, move, and format these images as needed. The following image formats are supported:

    • Bitmap (*.bmp, *.dib)
    • JPEG File Interchange Format (*.jpg, *.jpeg)
    • Portable Network Graphics (*.png)
    • Graphics Interchange Format (*.gif)
    • Tagged Image File Format (*.tif, *.tiff)
    • Microsoft Enhanced Metafile (*.emf)
    • Windows Metafile (*.wmf)
    • Scalable Vector Graphics (*.svg)

    Insert a Picture Shape

    Call the ShapeCollection.AddImage(DXImage, ShapeAnchorType) method to insert a picture into the worksheet. The ShapeAnchorType parameter specifies how the image is positioned and resized relative to the cells.

    If the anchor type is Absolute or TopLeftCell, the picture shape size matches the image size and takes the image resolution (DPI) into account. For example, a 200 × 100 pixel image with a resolution of 96 DPI produces a 625 × 312.5 picture shape in document units (1/300 of an inch). The same image with a resolution of 192 DPI produces a picture shape that is half as large.

    If the anchor type is TwoCells, the API anchors the picture to a default cell range regardless of the image size and resolution.

    The following code snippet inserts a picture from the Image.png file and anchors the picture corners to cells B2 and F12:

    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Spreadsheet;
    using DevExpress.Drawing;
    
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    // Load an image from a file.
    using FileStream imageStream = File.OpenRead("Image.png");
    DXImage image = DXImage.FromStream(imageStream);
    
    // Insert the picture and anchor it to cells B2 and F12.
    PictureShape picture = worksheet.Shapes.AddImage(image, ShapeAnchorType.TwoCells);
    TwoCellAnchor anchor = picture.ShapeAnchor.AsTwoCellAnchor();
    anchor.SetCorners(1, 1, 11, 5);
    
    Directory.CreateDirectory("output");
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream, DocumentFormat.Xlsx);
    

    Obtain and Format Pictures

    Call the ShapeCollection.GetImageShapes method to obtain all picture shapes in a worksheet. You can rotate, resize, format, replace, and remove these pictures as required.

    Crop and Rotate Pictures

    The following properties allow you to crop and rotate picture shapes:

    Property Description
    ShapeWithRotation.RotationAngle Specifies the rotation angle of the picture.
    PictureShape.CropTop Specifies the amount (in percentages) cropped from the top of the picture.
    PictureShape.CropBottom Specifies the amount (in percentages) cropped from the bottom of the picture.
    PictureShape.CropLeft Specifies the amount (in percentages) cropped from the left of the picture.
    PictureShape.CropRight Specifies the amount (in percentages) cropped from the right of the picture.

    The following code snippet inserts a picture from the Image.png file into a worksheet, crops 10 percent from each side of the picture, and rotates the picture by 15 degrees:

    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    using DevExpress.Drawing;
    
    Directory.CreateDirectory("output");
    
    using Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    using FileStream imageStream = File.OpenRead("Image.png");
    PictureShape picture = worksheet.Shapes.AddImage(DXImage.FromStream(imageStream));
    
    // Crop 10 percent from each side of the picture.
    picture.CropLeft = 10;
    picture.CropTop = 10;
    picture.CropRight = 10;
    picture.CropBottom = 10;
    
    // Rotate the picture.
    picture.RotationAngle = 15;
    
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream);
    

    Change Picture Z-Order

    You can use the following methods to change the Z-order of a picture (bring it to the front or send it to the back):

    Method Description
    ShapeCollection.BringForward(shape) Brings the shape one level forward in the Z-order.
    ShapeCollection.BringToFront(shape) Brings the shape to the front of the Z-order.
    ShapeCollection.SendBackward(shape) Sends the shape one level backward in the Z-order.
    ShapeCollection.SendToBack(shape) Sends the shape to the back of the Z-order.

    Format Pictures

    You can add borders to a picture, change its fill color, and apply other formatting options using the following properties:

    Property Description
    StyledShape.Outline Specifies the outline style of the picture.
    FilledShape.Fill Specifies the fill parameters.

    The following code snippet sets the outline style and fill parameters for a picture shape:

    using System.Drawing;
    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Spreadsheet;
    using DevExpress.Drawing;
    
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    // Insert a picture and anchor it to cells B2 and F12.
    using FileStream imageStream = File.OpenRead("Image.png");
    DXImage image = DXImage.FromStream(imageStream);
    PictureShape picture = worksheet.Shapes.AddImage(image, ShapeAnchorType.TwoCells);
    TwoCellAnchor anchor = picture.ShapeAnchor.AsTwoCellAnchor();
    anchor.SetCorners(1, 1, 11, 5);
    
    // Add a dashed dark blue outline to the picture.
    picture.Outline = new LineStyle() {
        Fill = new SolidFill(Color.DarkBlue),
        Width = 6,
        DashType = LineDashType.Dash
    };
    
    // Fill the picture background with a light yellow color.
    picture.Fill = new SolidFill(Color.LightYellow);
    
    Directory.CreateDirectory("output");
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream, DocumentFormat.Xlsx);
    

    Specify Alternative Text

    Assistive technologies (for example, screen readers) read alternative text to describe pictures to users. Use the following properties to specify accessibility settings for a picture:

    Property Description
    ShapeBase.AlternativeText Gets or sets alternative text that describes the drawing object for assistive technologies.
    ShapeBase.Decorative Gets or sets whether the drawing object is decorative.

    Specify alternative text for pictures that contain meaningful content. Mark pictures that do not contain meaningful content (for example, borders or background images) as decorative. Assistive technologies can skip decorative pictures.

    The following code snippet inserts two pictures, specifies alternative text for the first picture, and marks the second picture as decorative:

    using System.IO;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Spreadsheet;
    using DevExpress.Drawing;
    
    Workbook workbook = new Workbook();
    Worksheet worksheet = workbook.Worksheets[0];
    
    using FileStream imageStream = File.OpenRead("Image.png");
    DXImage image = DXImage.FromStream(imageStream);
    
    // Insert a picture and describe its content.
    PictureShape picture = worksheet.Shapes.AddImage(image);
    picture.AlternativeText = "Sales growth chart for 2026";
    
    // Insert a decorative picture.
    PictureShape decorativePicture = worksheet.Shapes.AddImage(image);
    decorativePicture.ShapeAnchor.AsAbsoluteAnchor().X = 600;
    decorativePicture.Decorative = true;
    
    Directory.CreateDirectory("output");
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream, DocumentFormat.Xlsx);
    

    Remove Pictures

    Call the ShapeCollection.Remove method to remove a picture from a worksheet - the ShapeCollection.GetImageShapes method returns all picture shapes in the worksheet. You can iterate through the retrieved list and remove each picture.

    The following code snippet loads a workbook from the Document.xlsx file and removes all pictures from the first worksheet. Other shapes remain on the worksheet:

    using System.IO;
    using System.Linq;
    using DevExpress.Docs.Office;
    using DevExpress.Docs.Spreadsheet;
    
    using FileStream input = File.OpenRead("Document.xlsx");
    using Workbook workbook = new Workbook(input);
    Worksheet worksheet = workbook.Worksheets[0];
    
    // Remove all pictures from the worksheet.
    // Call ToList to copy the sequence before you modify the collection.
    foreach (PictureShape picture in worksheet.Shapes.GetImageShapes().ToList())
        worksheet.Shapes.Remove(picture);
    
    Directory.CreateDirectory("output");
    using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
    workbook.Save(stream, DocumentFormat.Xlsx);
    

    Insert an In-Cell Picture

    You can place a picture in a cell. Unlike a picture shape, an in-cell picture is cell content. If you assign a value or formula to the cell, the API removes the picture.

    Call the Cell.SetImage(image) method to place a picture in a cell. You can pass a DXImage object to the SetImage method.

    The following code snippet places a picture from the Image.png file in cells B2 and B3 and specifies alternative text for the picture in cell B3:

    DevExpress Spreadsheet Document API - In-Cell Image

    using System.IO;
    using DevExpress.Docs.Spreadsheet;
    using DevExpress.Drawing;
    
    // Create a workbook and access the first worksheet.
    using Workbook workbook = new Workbook();
    
    Worksheet worksheet = workbook.Worksheets[0];
    
    // Load an image from a file.
    using FileStream imageStream = File.OpenRead("logo.png");
    DXImage image = DXImage.FromStream(imageStream);
    
    // Place the picture in cell B2.
    worksheet.Cells["B2"].SetImage(image);
    
    // Place the picture in cell B3 and specify alternative text.
    worksheet.Cells["B3"].SetImage(new CellImage(image, "logo image"));
    worksheet.Cells["B3"].Format.Alignment = new AlignmentFormat ( SpreadsheetHorizontalAlignment.Center, SpreadsheetVerticalAlignment.Center );
    

    Use the Cell.HasImage property to check whether a cell contains a picture. Call the Cell.GetImage() method to get picture and accessibility settings (CellImage.AltText and CellImage.IsDecorative properties).

    The Cell.Value property of a cell with a picture gets the #VALUE! error value. A formula that references the cell also returns #VALUE!.

    To remove a picture from a cell, call the Cell.Clear method.

    Note

    XLSX, XLSM, XLTX, XLTM, and XLSB formats support in-cell pictures. Other formats (for example, XLS and CSV) do not save in-cell pictures.