officefileapi-113743-spreadsheet-document-api-examples-pictures-how-to-insert-and-delete-pictures.md
This example demonstrates how to use the PictureCollection.AddPicture method overloads to insert a picture into a worksheet from different sources.
The PictureCollection.GetPicturesByName method enables you to obtain all pictures with the specified name. Call the PictureCollection.GetPictureById method to find a picture by its unique ID.
To remove a picture from a worksheet, use the picture’s Shape.Delete method.
The Spreadsheet Document API supports the following types of images.
SVG images are preserved as vector graphics when you export documents to Open XML formats (XLSX, XLSM, XLTX, and XLTM). When exporting to non-Open XML formats, SVG images are rasterized.
Spreadsheet Document API does not support pictures embedded in cells.
workbook.BeginUpdate();
// Set the measurement unit to Millimeter.
workbook.Unit = DevExpress.Office.DocumentUnit.Millimeter;
try
{
Worksheet worksheet = workbook.Worksheets[0];
// Insert a picture from a file so that its top left corner is in the specified cell.
// By default the picture is named Picture 1.. Picture NN.
worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", worksheet.Cells["A1"]);
// Insert a picture at 70 mm from the left, 40 mm from the top,
// and resize it to a width of 85 mm and a height of 25 mm, locking the aspect ratio.
worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", 70, 40, 85, 25, true);
// Insert the picture to be removed.
worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", 0, 0);
// Remove the last inserted picture.
// Find the shape by its name. The method returns a collection of shapes with the same name.
Picture picShape = worksheet.Pictures.GetPicturesByName("Picture 3")[0];
picShape.Delete();
}
finally
{
workbook.EndUpdate();
}
workbook.BeginUpdate()
' Set the measurement unit to Millimeter.
workbook.Unit = DevExpress.Office.DocumentUnit.Millimeter
Try
Dim worksheet As Worksheet = workbook.Worksheets(0)
' Insert a picture from a file so that its top left corner is in the specified cell.
' By default the picture is named Picture 1.. Picture NN.
worksheet.Pictures.AddPicture("Pictures\x-docserver.png", worksheet.Cells("A1"))
' Insert a picture at 70 mm from the left, 40 mm from the top,
' and resize it to a width of 85 mm and a height of 25 mm, locking the aspect ratio.
worksheet.Pictures.AddPicture("Pictures\x-docserver.png", 70, 40, 85, 25, True)
' Insert the picture to be removed.
worksheet.Pictures.AddPicture("Pictures\x-docserver.png", 0, 0)
' Remove the last inserted picture.
' Find the shape by its name. The method returns a collection of shapes with the same name.
Dim picShape As Picture = worksheet.Pictures.GetPicturesByName("Picture 3")(0)
picShape.Delete()
Finally
workbook.EndUpdate()
End Try
string imageUri = "https://www.devexpress.com/Products/NET/Controls/WinForms/spreadsheet/i/winforms-spreadsheet-control.png";
// Create an image from Uri.
SpreadsheetImageSource imageSource = SpreadsheetImageSource.FromUri(imageUri, workbook);
// Set the measurement unit to point.
workbook.Unit = DevExpress.Office.DocumentUnit.Point;
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets[0];
// Insert a picture from the SpreadsheetImageSource at 100 pt from the left, 40 pt from the top,
// and resize it to a width of 300 pt and a height of 200 pt.
worksheet.Pictures.AddPicture(imageSource, 100, 40, 300, 200);
}
finally
{
workbook.EndUpdate();
}
Dim imageUri As String = "https://www.devexpress.com/Products/NET/Controls/WinForms/spreadsheet/i/winforms-spreadsheet-control.png"
' Create an image from Uri.
Dim imageSource As SpreadsheetImageSource = SpreadsheetImageSource.FromUri(imageUri, workbook)
' Set the measurement unit to point.
workbook.Unit = DevExpress.Office.DocumentUnit.Point
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets(0)
' Insert a picture from the SpreadsheetImageSource at 100 pt from the left, 40 pt from the top,
' and resize it to a width of 300 pt and a height of 200 pt.
worksheet.Pictures.AddPicture(imageSource, 100, 40, 300, 200)
Finally
workbook.EndUpdate()
End Try
See Also