Other API Samples

Live SpreadsheetGear API Samples

Shapes Cropped Picture

Adds a picture consisting of the SpreadsheetGear logo on the left and text "SpreadsheetGear" on the right, then crops the "SpreadsheetGear" text out of the picture, leaving just the logo.

// Create a new workbook and get a reference to the active worksheet
SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
SpreadsheetGear.IWorksheet worksheet = workbook.ActiveWorksheet;

// This picture consists of the SpreadsheetGear logo and the text "SpreadsheetGear" to 
// the right of the logo.
string filename = "SG-Logo-Text.png";

// Add the picture to the worksheet, specifying the size (in this case 7" x 1" which is
// the original dimensions of the picture).  Note all units are measured in Points, where
// 1 inch == 72 Points.
SpreadsheetGear.Shapes.IShape shape = worksheet.Shapes.AddPicture(filename, 10, 10,
    7 * 72, 1 * 72);

// Get the IPictureFormat object from the IShape, which has various picture-related APIs, 
// including cropping APIs.
SpreadsheetGear.Shapes.IPictureFormat picture = shape.PictureFormat;

// We pre-measured the width of just the logo which has a width of 1.35 inches starting 
// from the left edge.  The full picture is 7 inches long, so we should crop from the 
// right edge of the picture a distance of 7" - 1.35".  Again, this API is measured in 
// Points, so we also multiply by 72.
picture.CropRight = (7.0 - 1.35) * 72;

// Additional Notes on Cropping:
//   - IPictureFormat also has the properties CropLeft, CropTop and CropBottom.
//   - Specify crop measurements based on the dimensions of the *original picture size*
//     and not the dimensions that you specified for the shape (IShape.Width / Height).
//   - Negative crop values can be used to extend the shape's boundary beyond the picture.

// Add a second uncropped copy of this picture below the cropped
// picture for comparison purposes.
worksheet.Shapes.AddPicture(filename, 10, 100);
Run This Sample
Render Image

Generate an image representation of the results of this sample, which uses the SpreadsheetGear.Drawing.Image class to convert ranges, charts and shapes to images.

Download File

Download an Excel Open XML Workbook (*.xlsx) file with the results of this sample.

Supporting Files

The following files are utilized by this sample: