Free Support Forum - aspose.com

Picture size and alignment

I am loading a picture in a cell like this:

objSheet.Cells.Merge(0, 0, 1, 5)
objSheet.Cells.SetRowHeightPixel(0, 75)
Dim intPicture As Integer = objSheet.Pictures.Add(0, 0, "C:\Image.gif")
Dim objPicture As Picture = objSheet.Pictures(intPicture)
objPicture.HeightInch = 0.64
objPicture.WidthInch = 4.02

1. As you can see, I give exact height and width but the image appears with a width of 9.1. Is there a way to uncheck the "lock aspect ratio" setting?

2. Is it possible to center the picture horizontally into the merged cells?

Hi,

1, Please try this fix. See following codes:

Workbook workbook = new Workbook();

Worksheet objSheet = workbook.Worksheets[0];

objSheet.Pictures.Add(0, 1, @"F:\FileTemp\test.bmp");
objSheet.Cells.Merge(0, 0, 1, 5);
objSheet.Cells.SetRowHeightPixel(0, 75);

Aspose.Cells.Picture objPicture = objSheet.Pictures[1];
objPicture.HeightInch = 0.64;
objPicture.WidthInch = 4.02;

objPicture.IsLockAspectRatio = false;

workbook.Save(@"F:\FileTemp\dest.xls");

2, Please use Picture.UpperLeftColumn; Picture.UpperLeftRow; Picture Left; Picture .Top; to set the upper-left corner.

I have the error "Error 1 'IsLockAspectRatio' is not a member of 'Aspose.Cells.Picture'. C:\_Dev\AsposeTest\AsposeTest\Form1.vb 86 13 AsposeTest"

I am using the trial of Aspose.Cells version 4.4.0.0 in a VB 2005 Windows application

I have also made the test in C# (because I see your example in C#) and it is not working (as I was expecting).

Hi,

Thanks for considering Aspose.

Well, Version 4.4.0 does not include Picture.IsLockAspectRatio property. Please either use the version posted by Warren in this thread or download the lastest version @: http://www.aspose.com/Community/Files/51/aspose.cells/entry111612.aspx and use it.

Thank you.

Ok I have downloaded version 4.4.1 and the property is now working. I also needed to use the Placement property to stop the picture having its size changing when a column is resized.

>>2, Please use Picture.UpperLeftColumn; Picture.UpperLeftRow; Picture Left; Picture .Top; to set the upper-left corner.

I try to center the picture in the merged cells. When I try objSheet.Cells("A1"), I cannot get the width.

How can I center the picture in the 5 columns merged?

Hi,

I also needed to use the Placement property to stop the picture having its size changing when a column is resized.

Well, you may use Picture.Placement property to set it to PlacementType.FreeFloating.

I try to center the picture in the merged cells. When I try objSheet.Cells("A1"), I cannot get the width. How can I center the picture in the 5 columns merged?

Suppose you have A1:E5 merged cells. When you get the width of the A1 cell the width of the column refers to A column only, this functionality is same as MS Excel. So, you have to use your own code to centralize your picture into the merged cell area.

E.g.,

Workbook workbook = new Workbook();
Worksheet objSheet = workbook.Worksheets[0];
objSheet.Pictures.Add(0, 1, @"d:\test\school.jpg");
objSheet.Cells.Merge(0, 0, 1, 5);
objSheet.Cells.SetRowHeightPixel(0, 179);

Aspose.Cells.Picture objPicture = objSheet.Pictures[0];
objPicture.HeightInch = 1.65;
objPicture.WidthInch = 3.02;
objPicture.Placement = PlacementType.FreeFloating;
objPicture.IsLockAspectRatio = false;
objPicture.UpperLeftColumn = 0;
objPicture.UpperLeftRow = 0;
objPicture.Top = 10;
objPicture.Left = 16;

workbook.Save(@"d:\test\dest_picbook.xls");
Thank you.

Ok, so I need to loop through the columns to calculate the total width.

But then, when I try to set the Left property of the Picture, I sometime receive this exception:

The left value must be less than the upper left column width.

I found that this error occurs when the first column is narrower then the left property.

So I have written that code to loop through the column width a second time to place the picture in the correct cell.

'------------------------------------------------------------------

'--- Center the image in the middle of the merge cells

'------------------------------------------------------------------

'Find the width (in pixels) of the merge cells

Dim intWidth As Integer = 0

For intI As Integer = 0 To 4

intWidth += objSheet.Cells.GetColumnWidthPixel(intI)

Next

'Calculate the LEFT position of the image

Dim objPicture As Picture = objSheet.Pictures(intPictureHeader)

Dim intLeft As Integer

If intWidth > objPicture.Width Then

intLeft = (intWidth - objPicture.Width) \ 2

Else

intLeft = 0

End If

'The Left property must abolutely be related to the UpperLeftColumn

For intI As Integer = 0 To 4

If objSheet.Cells.GetColumnWidthPixel(intI) > intLeft Then

objPicture.Left = intLeft

Exit For

Else

intLeft -= objSheet.Cells.GetColumnWidthPixel(intI)

objPicture.UpperLeftColumn += 1

End If

Next

Is there an easier way?