Adding Image to Cells File

How to add image with absolute position on document using Aspose.Cells .Net version?

@chub,

Please try ShapeCollection.AddFreeFloatingShape() for your requirements.

@chub,
In addition to above-suggested function by @Amjad_Sahi, you can also position the pictures absolutely by using the Left and Top properties of the Picture object. Following sample code places an image in cell F6, 60 pixels from the left and 10 pixels from the top of the cell. Please give it a try and share the feedback.

// Instantiating a Workbook object
Workbook workbook = new Workbook();

// Adding a new worksheet to the Workbook object
int sheetIndex = workbook.Worksheets.Add();

// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];

// Adding a picture at the location of a cell whose row and column indices
// Are 5 in the worksheet. It is "F6" cell
int pictureIndex = worksheet.Pictures.Add(5, 5, dataDir + "logo.jpg");

// Accessing the newly added picture
Aspose.Cells.Drawing.Picture picture = worksheet.Pictures[pictureIndex];

// Absolute positioning of the picture in unit of pixels
picture.Left = 60;
picture.Top = 10;

// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls");

You may also visit the following article for more details on managing the pictures:
Managing Pictures

I Have some issue. Program works fine, but i can’t see inserted image and all cells are same size.
ZIP on Google Drive
And One more Question… Is it possible to insert image NOT based on Cell position? Just margin top and left, width and height.

@chub,

I could not download your attachment(s), it demands to login and request for permissions. Please do share the file properly in Google drive and then paste the shared link (for download), so we could download it seamlessly and evaluate your issue.

Please see the previous post:

Also, see the code segment for your reference:
e.g
Sample code:

...........

string path = @"E:\test2\picture in the header\BI.jpg";
            FileStream inFile = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            byte[] binaryData = new Byte[inFile.Length];
            long bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
int top, left, height, width;

            //Get the first worksheet.
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Shapes.AddFreeFloatingShape(MsoDrawingType.Picture, top, left, height, width, binaryData, true); 
........

Hope, this helps a bit.

1 Like