Scale picture

Hi,

I want to insert and scale a picture but leave it on the first row. I tried the following:

int temp = ws.Pictures.Add(0, 4, "zp_logo.jpg");

ws.Pictures[temp].HeightScale = 50;

ws.Pictures[temp].WidthScale = 50;

ws.Pictures[temp].Move(0, 4);

The first 3 lines work as expected and scale the picture. But then I want to move it back up again as the scaling apparently takes the lower left corner as reference (while the add method takes top left as reference btw), but the 4th line doesn't move the picture but scales it back to 100% height. How to move the picture?

Thank you, best,

-Hendri.

In an attempt to work around this problem, I scaled the picture myself first and try to insert that. But when inserted, it scales the width to 178% and I cannot change that from aspose, in other words:

int picIndex = ws.Pictures.Add(0, 1, exeDir + "\\zp_logo_small.jpg");

ws.Pictures[picIndex].WidthScale = 100;

does not work.

Any ideas?

And when inserting works properly, I would actually like to position it with the top right corner instead of the top left corner. The picture is on top of cells B1 and C1 and I would like to align the right side to the right border of C1. Is that possible?

Thank you, best,

-Hendri.

And

int picIndex = ws.Pictures.Add(0, 1, exeDir + "\\zp_logo_small.jpg");

ws.Pictures[picIndex].HeightScale = 100;

ws.Pictures[picIndex].WidthScale = 100;

Makes the picture 0% high... Please help.

-Hendri.

Hi,

Could you post your jpg file and your DPI Info?

We are looking into how position the picture to top right corner.

Hi Hendri,

Please try this attached fix. And you can specify the scale using following method:

public int Add(int upperLeftRow, int upperLeftColumn, string fileName, int widthScale, int heightScale)

Hi,

the jpg's are attached, 72 dpi both. First I tried zp_logo and scale that with aspose. It was included properly, but I can't scale and move it. Then I tried zp_logo_small, but I can't even add that properly, when added, width =178%, no way to chagne that. Setting height to 100, makes it 0%.

Further, as a workaround for using top right as reference, can aspose do the following (when knowing in advance that the pic (when properly scaled) is on top of B1 and C1, which I do know)?

pic.leftoffset=column(B).width+column(C).width-pic.width;

with leftoffset relative to left border of B1.

Thank you, best,

-Hendri.

Hi Laurence,

yes, that works, thank you. I found the other method, but it had the same effects.

But as before, I do need to run it on a 64 bit machine, so could you please send me a .net2 version?

Thank you, best regards,

-Hendri.

Hi -Hendri,

We will attach the fix (Aspose.Cells.dll) for .Net 2.0 version soon.

Thank you.

Ok, thank you, could it be before tomorrow please? I have to take it in production then.

And the aligning that I proposed? I tried the following, but it doens't do anything.

Workbook wb = new Workbook();

Worksheet ws = wb.Worksheets["Sheet1"];

Cells cell = ws.Cells;

cell.SetColumnWidth(0, 45);

cell.SetColumnWidth(1, 28);

int temp = ws.Pictures.Add(0, 1, "zp_logo.jpg", 50, 50);

ws.Pictures[temp].Left = cell.GetColumnWidthPixel(0) + cell.GetColumnWidthPixel(1) - ws.Pictures[temp].Width;

wb.Save("test.xls");

Thank you, best,

-Hendri.

Hi,

We will try to send you ASAP.

For aligning the picture, May the following code give you some insight, I tried it and it works fine:

Workbook workbook = new Workbook ();
Worksheet ws = workbook.Worksheets[0];
int temp = ws.Pictures.Add(0, 2, "d:\\test\\zp_logo.jpg",50,50);
Picture pic = ws.Pictures[0];
int picwidth = pic.OriginalWidth;
int c = ws.Cells.GetColumnWidthPixel(2);
int d = ws.Cells.GetColumnWidthPixel(3);
int offset = picwidth - (c+d);
pic.UpperDeltaX = offset;
workbook.Save("d:\\test\\bkzp_logo.xls");

Thank you.

> We will try to send you ASAP.

Ok, thank you.

> For aligning the picture, May the following code give you some insight, I tried it and it works fine:

It doesn't, sorry. It squeezes the pic and doesn't move it. So the left edge of the pic is moved to the right, but the right edge doesn't move so the pic gets squeezed. I'm using 4.3.0.4 .net1 that I got from Laurence in this thread.

I hope you can repair it.

Thank you, best,

-Hendri.

BTW, the following is the test code. Maybe it seemed to work for you as you didn't change column widths as I did in the example that I try to get working.

Workbook wb = new Workbook();

Worksheet ws = wb.Worksheets["Sheet1"];

Cells cell = ws.Cells;

cell.SetColumnWidth(0, 45);

cell.SetColumnWidth(1, 28);

int temp = ws.Pictures.Add(0, 1, "zp_logo.jpg", 50, 50);

Picture pic = ws.Pictures[temp];

int picwidth = pic.OriginalWidth;

int c = ws.Cells.GetColumnWidthPixel(2);

int d = ws.Cells.GetColumnWidthPixel(3);

int offset = picwidth - (c + d);

pic.UpperDeltaX = offset;

wb.Save("test.xls");

Hi Hendri,

Could you create a sample spreadsheet using MS Excel and place your pic with your desired alignment and scaling factor and post us here, so that we may be able to figure our your issue according to your requirement.

Thank you.

Hi,

it's attached. There is something funny still going on with scaling. I imported the full logo, but scaled it. The height is 78 originally, 39 pixels when imported. That's fine (test1.xls). But I'd prefer to scale the picture beforehand to avoid including redundant things and didn't scale it, but now the picture is imported much smaller (test2.xls). I can live with test1.xls for now. (Logo's also attached)

Import done with

int temp = ws.Pictures.Add(0, 1, "zp_logo.jpg",50,50);

(test1) and

int temp = ws.Pictures.Add(0, 1, "zp_logo_small.jpg");

(test2) respectively.

Notice that I adjusted the alignment manually to align right to column C. I hope this can be automated, depending on column B and C width (which might change in the future).

Thank you, best,

-Hendri.

I see where the problem is with respect to the scaling: when inserting the zp_logo in excel (2003), it comes out 59 pixels high but it was 78. Does Excel use 95 dpi or so?

When scaling it to 50%, it does come out as 30 pixels high, just as when inserting the prescaled zp_logo_small with aspose.

So the strange thing comes in with the Add method of aspose. It doesn't scale as Excel does. Somehow, excel applies a scaling to every pic included, maybe due to dpi differences. Aspose does so too, but only when not specifying a scaling in the Add method.

But anyway, knowing this, we can find a workaround such that the pic comes out as we want. I hope you already have any success on right aligning it.

Thank you, best,

-Hendri.

Ugh, it also depends on the file... So making a workaround becomes harder...

The following 2 lines should work in the same way:

int temp = ws.Pictures.Add(0, 1, "zp_logo.jpg");

temp = ws.Pictures.Add(5, 1, "zp_logo_small.jpg");

For clarity, the pics are 78 and 39 high respectively.

The top one produces a pic of 78 high, the bottom one of 30 with aspose.

Inserting with excel produces a pic of 59 high and 30 high respectively.

You lost me there.

-Hendri.

Hi,

Please try the attached fix.

1,DPI , We use 96 DPI as the default DPI as MS Excel. I have checked your image and found the image had not Real DPI.Maybe you have to set HorizontalResolution and VerticalResolution in the image file.

2,Left,Height, We have fixed the problem.

3,Align to top-right corner, example: if the top-right corner is cell "G1",Please use method Picture. AlignTopRightCorner(0,6);

4,Please ignore Amjad's post about UpperDeltaX. DeltaX is in unit of 1/1024 of the width of the column. DeltaY is in unit of 1/256 of the height of the row.

5.Property LowerRightRow is used to set the lower row index; Property LowerDeltaY is used to set vertical offset from its lower row;

Property LowerRightColumn is used to set the right column index; Property LowerDeltaX is used to set horizonal offset from its right column;

Hi Hendri,

What do you mean making a workaround.

Please add the two images with the following codes in such workaround:

int temp = ws.Pictures.Add(0, 1, "zp_logo.jpg");

temp = ws.Pictures.Add(5, 1, "zp_logo_small.jpg");

And post your created file and the two images.

Hi Warren,

thanks, it works nicely. Could you please make a net2 version available so that I can use it? Thank you.

1) Hm, strange, I saved both pics with photoshop, in the same way.

2) Good.

3) Sounds nice, thanks.

4/5) Ok, I noticed too that it didn't work.

Thanks, best,

-Hendri.

> And post your created file and the two images.

I described the problem before. I attached the file and images again, but I did (with 4.3.0.5):

int temp = ws.Pictures.Add(0, 1, "zp_logo.jpg",50,50);

ws.Pictures[temp].AlignTopRightCorner(0, 2);

temp = ws.Pictures.Add(5, 1, "zp_logo_small.jpg");

You will see the difference in size while the small version is 50% of the normal version in windows and photoshop, it isn't in excel, but it might be a problem with the pics, but excel does treat them differently than aspose, see my post before in this thread.

Thanks, best,

-Hendri.