# Picture in Cell

## Content

DsExcel .NET allows you to embed images directly into worksheet cells, similar to MsExcel’s native functionality. Pictures in cells are anchored to the cell, and an image icon appears next to the cell to represent the data. The picture will move, resize, or be deleted in sync with the cell. The pictures in cells are supported when exporting to PDF, HTML, and image formats.

## Add Picture in Cell

You can embed images into cells using the [IRange.CellPicture](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.CellPicture.html) property. To add a picture, create a [CellPicture](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.CellPicture.html) object containing the image data and optional alt text, and assign it to the cell.
The alt text serves as the display value for features that require text evaluation, such as filter and sort. If the alt text is empty, a localized default value will be used (“Picture” in English, “图片” in Chinese, “画像” in Japanese, “그림” in Korean, or “Bild” in German). You can use the [CellPicture.AltText](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.CellPicture.AltText.html) property to get or set the alt text.
The following example demonstrates how to add a picture with alt text to a cell.

```csharp
// Create a new workbook.
Workbook workbook = new Workbook();
IWorksheet sheet = workbook.ActiveSheet;

// Read image data from a file.
byte[] imageData = File.ReadAllBytes("Picture1.png");

// Set picture in cell with alt text.
sheet.Range["A1"].CellPicture = new CellPicture(imageData, "Dress");

// Save to an Excel file.
workbook.Save("PictureinCell.xlsx");
```

The output is shown in the figure below:
![image](https://cdn.mescius.io/document-site-files/images/6c089d65-4816-4591-8c0d-bb0bfb8c43df/image-20260414.3b2144.png)

>type=note
> **Note**: When a cell contains a picture in cell, [IRange.Value](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.Value.html) returns [CalcError.Value](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.CalcError.html), and [IRange.Text](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.Text.html) returns an empty string.

## Retrieve Picture from Cell

Use the [IRange.CellPicture](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.CellPicture.html) property to retrieve a picture from a cell. This property returns a [CellPicture](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.CellPicture.html) object if the cell contains a picture in cell; otherwise, it returns null. This property also supports reading valid pictures generated by the IMAGE() function.
The [CellPicture.ImageData](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.CellPicture.ImageData.html) (byte[]) property returns a copy of the image’s binary data. Supported formats include PNG, JPG, GIF, BMP, and SVG. Modifying the returned array does not affect the image stored in the cell.
The following example demonstrates how to retrieve picture data from a cell and save it to a png file.

```csharp
// Create a new workbook.
Workbook workbook = new Workbook();
IWorksheet sheet = workbook.ActiveSheet;

// Read image data from file.
byte[] imageData = File.ReadAllBytes("Picture1.png");

// Set picture in cell with alt text.
sheet.Range["A1"].CellPicture = new CellPicture(imageData, "Dress");

// Retrieve the picture from the cell.
CellPicture picture = sheet.Range["A1"].CellPicture;

if (picture != null)
{
    // Get the alt text.
    string altText = picture.AltText;
    Console.WriteLine($"Alt Text: {altText}");  // Output: Alt Text: Dress

    // Get the image data.
    byte[] retrievedData = picture.ImageData;

    // Save to a file.
    File.WriteAllBytes("retrieved_picture.png", retrievedData);

}
```

## Modify Alt Text

The returned [CellPicture](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.CellPicture.html) object is a copy of the cell’s data. Modifying this object does not automatically update the cell. To change the picture content or alt text in the cell, create or modify a [CellPicture](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.CellPicture.html) and assign it back to the [IRange.CellPicture](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.CellPicture.html) property.
The following example demonstrates how to modify a picture in a cell.

```csharp
// Create a new workbook.
Workbook workbook = new Workbook();
IWorksheet sheet = workbook.ActiveSheet;

// Read image data from file.
byte[] imageData = File.ReadAllBytes("Picture1.png");

// Set picture in cell with alt text.
sheet.Range["A1"].CellPicture = new CellPicture(imageData, "Dress");

// Get the existing picture.
CellPicture existingPicture = sheet.Range["A1"].CellPicture;

// Modify alt text and reassign.
existingPicture.AltText = "Pink dress with sparkles";
sheet.Range["A1"].CellPicture = existingPicture;

// Save to an Excel file.
workbook.Save("ModifyPictureinCell.xlsx");
```

The output is shown in the figure below:
![image](https://cdn.mescius.io/document-site-files/images/6c089d65-4816-4591-8c0d-bb0bfb8c43df/image-20260414.994f5a.png?width=806)

## Remove Picture from Cell

You can remove a picture from a cell using any of the following methods. When applied to a range containing multiple cells, the picture in each cell will be removed.

1. Use the [IRange.Clear()](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.Clear.html) or [IRange.ClearContents()](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.ClearContents.html) method. [IRange.Clear()](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.Clear.html) removes the cell’s formatting and picture, whereas [IRange.ClearContents()](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.ClearContents.html) removes only the picture, and preserves the cell’s formatting.
2. Set the [IRange.CellPicture](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.CellPicture.html) property to null.
3. Assign any value to the [IRange.Value](/document-solutions/dot-net-excel-api/api/online/DS.Documents.Excel/GrapeCity.Documents.Excel.IRange.Value.html) property.

The following example demonstrates how to remove a picture from a cell.

```csharp
// Create a new workbook.
Workbook workbook = new Workbook();
IWorksheet sheet = workbook.ActiveSheet;

// Read image data from a file.
byte[] imageData = File.ReadAllBytes("Picture1.png");

// Set picture in cell with alt text.
sheet.Range["A1"].CellPicture = new CellPicture(imageData, "Dress");

// Method 1: Remove the picture using ClearContents().
sheet.Range["A1"].ClearContents();

// Method 2: Remove the picture by setting CellPicture to null.
sheet.Range["A1"].CellPicture = null;

// Method 3: Remove the picture by assigning a value to the cell.
sheet.Range["A1"].Value = "No picture";

// Save to an Excel file.
workbook.Save("RemovePicture.xlsx");
```