Other API Samples C# logo

Cropped Picture SpreadsheetGear API Sample

Description

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.

Sample Source Code

// 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.
byte[] imageBytes = System.IO.File.ReadAllBytes(@"Files\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.
var shape = worksheet.Shapes.AddPicture(imageBytes, 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(imageBytes, 10, 100, 7 * 72, 1* 72);