Back to Devexpress

PictureCollection.AddPicture(String, Single, Single) Method

officefileapi-devexpress-dot-spreadsheet-dot-picturecollection-dot-addpicture-x28-system-dot-string-system-dot-single-system-dot-single-x29.md

latest9.1 KB
Original Source

PictureCollection.AddPicture(String, Single, Single) Method

Inserts a picture from a file at the specified location.

Namespace : DevExpress.Spreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
Picture AddPicture(
    string filename,
    float x,
    float y
)
vb
Function AddPicture(
    filename As String,
    x As Single,
    y As Single
) As Picture

Parameters

NameTypeDescription
filenameString

A string that specifies a path to the image file.

| | x | Single |

A Single value that is the distance from the left of the worksheet. The ISpreadsheetComponent.Unit property specifies the unit of measurement.

| | y | Single |

A Single value that is the distance from the top of the worksheet. The ISpreadsheetComponent.Unit property specifies the unit of measurement.

|

Returns

TypeDescription
Picture

A Picture object that is the inserted picture.

|

Remarks

The image below illustrates the distances used to specify a picture’s placement on a worksheet.

The inserted picture has FloatingObject.Placement set to Placement.FreeFloating.

Example

View Example

vb
Dim imageStream As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Pictures.x-spreadsheet.png")
Dim imageSource As SpreadsheetImageSource = SpreadsheetImageSource.FromStream(imageStream)
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 Picture1.. PictureNN.
    worksheet.Pictures.AddPicture("Pictures\x-docserver.png", worksheet.Cells("D5"))
    ' Insert a picture to fit in the specified range.
    worksheet.Pictures.AddPicture("Pictures\x-docserver.png", worksheet.Range("B2"))
    ' Insert a picture from the SpreadsheetImageSource at 120 mm from the left, 80 mm from the top, 
    ' and resize it to a width of 70 mm and a height of 20 mm, locking the aspect ratio.
    worksheet.Pictures.AddPicture(imageSource, 120, 80, 70, 20, True)
    ' Insert the picture to be removed.
    worksheet.Pictures.AddPicture("Pictures\x-docserver.png", 0, 0)
    ' Remove the last inserted picture.
    ' Find the Picture by its name. The method returns a collection of Pictures with the same name.
    Dim pic As Picture = worksheet.Pictures.GetPicturesByName("Picture 4")(0)
    pic.Delete()
Finally
    workbook.EndUpdate()
End Try
csharp
Stream imageStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("SpreadsheetControl_API.Pictures.x-spreadsheet.png");
SpreadsheetImageSource imageSource = SpreadsheetImageSource.FromStream(imageStream);
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 Picture1.. PictureNN.
    worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", worksheet.Cells["D5"]);
    // Insert a picture to fit in the specified range.
    worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", worksheet.Range["B2"]);
    // Insert a picture from the SpreadsheetImageSource at 120 mm from the left, 80 mm from the top, 
    // and resize it to a width of 70 mm and a height of 20 mm, locking the aspect ratio.
    worksheet.Pictures.AddPicture(imageSource, 120, 80, 70, 20, true);
    // Insert the picture to be removed.
    worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", 0, 0);
    // Remove the last inserted picture.
    // Find the Picture by its name. The method returns a collection of Pictures with the same name.
    Picture pic = worksheet.Pictures.GetPicturesByName("Picture 4")[0];
    pic.Delete();
}
finally
{
    workbook.EndUpdate();
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the AddPicture(String, Single, Single) 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.

winforms-spreadsheetcontrol-api-part-2/CS/SpreadsheetControl_API_Part02/SpreadsheetActions/PictureActions.cs#L44

csharp
// Insert the picture to be removed.
worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", 0, 0);
// Remove the last inserted picture.

wpf-spreadsheetcontrol-api-part-2/CS/SpreadsheetControl_WPF_API_Part02/SpreadsheetActions/ShapeActions.cs#L38

csharp
// Insert the picture to be removed.
worksheet.Pictures.AddPicture("Pictures\\chart.png", 0, 0);
// Remove the last inserted picture.

spreadsheet-document-api-examples-part2/CS/SpreadsheetDocServerAPIPart2/CodeExamples/PictureActions.cs#L29

csharp
// Insert a picture.
worksheet.Pictures.AddPicture("Pictures\\x-docserver.png", 0, 0);
// Find the last inserted picture by its name.

winforms-spreadsheetcontrol-api-part-2/VB/SpreadsheetControl_API_Part02/SpreadsheetActions/PictureActions.vb#L45

vb
' Insert the picture to be removed.
worksheet.Pictures.AddPicture("Pictures\x-docserver.png", 0, 0)
' Remove the last inserted picture.

wpf-spreadsheetcontrol-api-part-2/VB/SpreadsheetControl_WPF_API_Part02/SpreadsheetActions/ShapeActions.vb#L39

vb
' Insert the picture to be removed.
worksheet.Pictures.AddPicture("Pictures\chart.png", 0, 0)
' Remove the last inserted picture.

spreadsheet-document-api-examples-part2/VB/SpreadsheetDocServerAPIPart2/CodeExamples/PictureActions.vb#L25

vb
' Insert a picture.
worksheet.Pictures.AddPicture("Pictures\x-docserver.png", 0, 0)
' Find the last inserted picture by its name.

See Also

Charts and Graphics in Spreadsheet Documents

How to: Insert and Delete Pictures

PictureCollection Interface

PictureCollection Members

DevExpress.Spreadsheet Namespace