Learn more about Xeed Workbooks for .NET
Now that we know the basics to making and editing our Workbook, including the use of Tables, Styles and SheetViews, let’s look into adding a new element: Pictures.
What is a Picture?
A Picture represents an image element in the Workbook, it is also referred to as a drawing element.
The Picture class has the following properties:
Description: the description of the picture.
DrawingClientData: the information of the ClientData, which is how the picture should behave when the worksheet is protected or printed.
PictureLocks: the information of the PictureLocks, which are the manipulations allowed on the picture.
Accessing Pictures
The pictures in a Workbook are accessed through the PictureCollection on a given Worksheet. The PictureCollection class will contain all the pictures in the Worksheet, and will also allow the user to add new pictures and manipulate them.
The PictureCollection class has the following properties:
Count: returns the number of Pictures in the current Worksheet.
Item: returns the Picture located at the specified index.
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
var worksheet = document.Worksheets[ 0 ];
var pictureQty = worksheet.Pictures.Count;
var firstPicture = worksheet.Pictures[ 0 ];
}
Adding Pictures to a Worksheet
To add pictures to a Worksheet, we use the Add method available on the PictureCollection class.
The Add method offers several overloads, allowing flexibility in the type of Picture to add and where it is positioned.
Note:
For the overloads that have a scale parameter, the value must be 1 or greater (100 by default).
Adding pictures to a Worksheet:
using( var document = Workbook.Load( "testDoc.xlsx" ));
{
var srcFile = PictureSampleResourcesDirectory + @"balloon.jpg";
var srcStream = new FileStream( PictureSampleResourcesDirectory + @"balloon.jpg", FileMode.Open, FileAccess.Read );
var worksheet = document.Worksheets[ 0 ];
// Overload 1: filename and scale
var pic1 = worksheet.Pictures.Add( srcFile, 50 );
// Overload 2: stream and scale
var pic2 = worksheet.Pictures.Add( srcStream, 50 );
// Overload 3: filename, top left position (coordinates), scale
var pic3 = worksheet.Pictures.Add( srcFile, 14, 0, 50 );
// Overload 4: stream, top left position (coordinates), scale
var pic4 = worksheet.Pictures.Add( srcStream, 14, 0, 50 );
// Overload 5: filename, top left position (address), scale
var pic5 = worksheet.Pictures.Add( srcFile, "A4", 50 );
// Overload 6: stream, top left position (address), scale
var pic6 = worksheet.Pictures.Add( srcStream, "A4", 50 );
// Overload 7: filename, top left and bottom right positions (coordinates)
var pic7 = worksheet.Pictures.Add( srcFile, 14, 0, 30, 15 );
// Overload 8: stream, top left and bottom right positions (coordinates)
var pic8 = worksheet.Pictures.Add( srcStream, 14, 0, 30, 15 );
// Overload 9: filename, top left and bottom right positions (addresses)
var pic9 = worksheet.Pictures.Add( srcFile, "A4", "E12");
// Overload 10: stream, top left and bottom right positions (addresses)
var pic10 = worksheet.Pictures.Add( srcStream, "A4", "E12" );
document.Save();
}
Setting restrictions on a Picture
The PictureLocks class allows the ability to remove the authorization to modify certain properties of the picture in Microsoft Excel. By default, all values are set to false.
Available settings (set them to true to apply their corresponding behavior):
NoAdjustHandles: removes the authorization to use the adjust handles.
NoChangeArrowheads: removes the authorization to change Arrowheads.
NoChangeAspect: removes the authorization to change the ratio.
NoChangeShapeType: removes the authorization to change the connection shape.
NoCrop: removes the authorization to crop the picture.
NoEditPoints: removes the authorization to edit the connection shape point.
NoGrp: removes the authorization to group shapes.
NoMove: removes the authorization to move the picture.
NoRot: removes the authorization to rotate the picture.
NoSelect: removes the authorization to select the picture.
For more information, please refer to the documentación.