Crop picture in excel

Hi

I have attached two pictures, the “Pic1” shows the picture on the grid is cropped to stay in the merge cells, when I export this picture to the excel using the following code, the picture will overflow out of the merge cells as you can see in the “Pic2”, how can I crop the picture look same as the “Pic1”? I have the information for the cropping height and width, I just don’t know which properties to set up.

Dim stream As IO.MemoryStream = New IO.MemoryStream(comp.Image)
Dim image As Image = image.FromStream(stream)
Dim imageHeight As Integer = image.Height
Dim imageWidth As Integer = image.Width
Dim pictureIndex As Integer= MyWorkSheet.Pictures.Add(comp.OwnedRange.Top, comp.OwnedRange.Left, stream)
Dim picture As Aspose.Cells.Drawing.Picture= MyWorkSheet.Pictures(pictureIndex)
picture.Width = imageWidth
picture.Height = imageHeight

Thanks,
Wei

Hi Wei,

Thanks for your posting and using Aspose.Cells for .NET.

Please use the following properties to crop image inside a worksheet for your needs.

  • Picture.FormatPicture.LeftCrop
  • Picture.FormatPicture.RightCrop
  • Picture.FormatPicture.TopCrop
  • Picture.FormatPicture.BottomCrop

Please also download and use the latest version: Aspose.Cells for .NET 7.7.2. If you face any issue, please feel free to ask. We will look into your issue and help you asap.

Hi,

Could you please provide an example how to use these properties? For example, if the image actual height and width are 400200(pixel), after cropped it will be 300200. how do I set up these four properties to achieve this?

Thanks,
Wei

Hi Wei,

Thanks for your posting and using Aspose.Cells.

I have looked into this issue and found Aspose.Cells does not change the size of crop but it expands/shrinks the image. Could you provide you sample source and expected xls/xlsx file with image one with crop and one without crop.

You can create both of them manually using MS-Excel and attach it here for our reference. We will log this issue in our database for a fix/enhancement.

Thanks for the update. I have attached two workbooks which "Pciture(without cropping)" is the currrent output Aspose support and "Picture(with cropping)" is what we expect.

Thanks,

Wei

Hi Wei,

Thanks for your files and using Aspose.Cells.

We will investigate how to crop the image with desired dimensions using Aspose.Cells APIs and update you asap.

Hi Wei,

Thanks for using Aspose.Cells.

You need to first crop the image using Picture.FormatPicture sub-properties and then adjust its height and width using Picutre.Height and Picture.Width properties.

Please check the source s1.xlsx (attached) file. In sheet1, it contains a cropped picture and in sheet2, it contains the uncropped picture. I then used the following code which read the values from picture1 and assigned to picture2. After the execution of code, the picture2 in output.xlsx (attached) matches perfectly.

Please compare the pics in sheet2 of s1.xlsx and output.xlsx.

In the screenshot, you can see the picture cropped by Aspose.Cells, which is exactly the same with the original cropped picture shown inside the screenshot for comparison.

C#


Workbook w1 = new Workbook(“s1.xlsx”);


Worksheet ws1 = w1.Worksheets[0];

Worksheet ws2 = w1.Worksheets[1];


Picture p1 = ws1.Pictures[0];

Picture p2 = ws2.Pictures[0];


//First crop the picture

p2.FormatPicture.BottomCrop = p1.FormatPicture.BottomCrop;

p2.FormatPicture.LeftCrop = p1.FormatPicture.LeftCrop;

p2.FormatPicture.RightCrop = p1.FormatPicture.RightCrop;

p2.FormatPicture.TopCrop = p1.FormatPicture.TopCrop;


//Reset the picture height and width

p2.Height = p1.Height;

p2.Width = p1.Width;


w1.Save(“output.xlsx”);

Screenshot:

Hi Shakeel ,

I have tried the solution you provide, it works great. Thanks for the quick update.

Wei

Hi,


Good to know that your issue is sorted out now. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

Thank you.