Free Support Forum - aspose.com

How can I add images to a spreadsheet and retain the original height/width ratio of the image?

Using the following code:

var ms = new MemoryStream();
Image _logo = RoboReporterConstsAndUtils.GetURLImage("http://www.proactusa.com/bla/pa_logo_notag.png");
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable - 1, ms);

...I'm able to put images on sheets, but not only do they not display at exactly the same size:

The original image is 199 pixels wide and 130 pixels high
The images generated using the code above, though, are 1.63" X 2.07" in one case, and and 1.67" X 2.07" in another.

...but also the ratio is off - the ratio of the original file (199X130) is about 1.5:1, while the ratio of the images displaying on the sheets are more like 1.25:1

Why is the height being stretched and/or the width being squashed, and how can I prevent this?

Hi,


Thanks for your posting and using Aspose.Cells.

We have tested your issue with the following sample code but found some problem in the output excel file. There must be something wrong with this code or there is some bug in the Aspose.Cells. We will look into this issue further and update you asap.

C#
Workbook wb = new Workbook();
wb.Worksheets.Add();

Worksheet ws1 = wb.Worksheets[0];
Worksheet ws2 = wb.Worksheets[1];

//Create two memory stream objects
//This is for first picture in sheet1
byte[] bts1 = File.ReadAllBytes(“p1.jpg”);
byte[] bts2 = File.ReadAllBytes(“p2.jpg”);

MemoryStream ms1 = new MemoryStream();
ms1.Write(bts1, 0, bts1.Length);
ms1.Position = 0;

//This is for second picture in sheet2
MemoryStream ms2 = new MemoryStream();
ms2.Write(bts2, 0, bts2.Length);
ms2.Position = 0;

//Add picture in first worksheet
int idx = ws1.Pictures.Add(1, 1, ms1);

//Add picture in second worksheet with original size
idx = ws2.Pictures.Add(1, 1, ms2);
Picture pic = ws2.Pictures[idx];
pic.HeightScale = 100;
pic.WidthScale = 100;

wb.Save(“ClayShannon.xlsx”);

Hi,


Thanks for your posting and using Aspose.Cells.

Please check the following sample code. It answers two of your questions.

1 - You can reuse same memory stream object containing your picture in your workbook and worksheets.

2 - How to add picture with original size? Please set WidthScale and HeightScale properties to 100 and it will add picture with original size.

The following code adds three picture by reusing the same memory stream object in each of three worksheets. The first picture is 10%, second is 30% and third is 100%. So the third worksheet contains the picture in its original size.

Please read the comments inside the code especially the two comments highlighted in red color for more help.

I have also attached the output excel file generated with the code and the picture used in the code for a reference.

C#
//Create Worbook object
Workbook wb = new Workbook();

//Add 2 more sheets
wb.Worksheets.Add();
wb.Worksheets.Add();

//Access all three sheets
Worksheet ws1 = wb.Worksheets[0];
Worksheet ws2 = wb.Worksheets[1];
Worksheet ws3 = wb.Worksheets[2];

//Read your picture in bytes
byte[] btsPic = File.ReadAllBytes(“p1.jpg”);

//Create a memory stream object which should hold your picture
//You can reuse the memorys stream object as much as you like
MemoryStream msPic = new MemoryStream();
msPic.Write(btsPic, 0, btsPic.Length);
msPic.Position = 0;

//Add memory stream picture in all worksheets
ws1.Pictures.Add(1, 1, msPic);
ws2.Pictures.Add(1, 1, msPic);
ws3.Pictures.Add(1, 1, msPic);

//Access all three pictures
Picture pic1 = ws1.Pictures[0];
Picture pic2 = ws2.Pictures[0];
Picture pic3 = ws3.Pictures[0];

//Set WidthScale and HeightScale, if you will set them 100, you will get original size of the picture
pic1.WidthScale = 10;
pic1.HeightScale = 10;

pic2.WidthScale = 30;
pic2.HeightScale = 30;

//This is original size of the picture
pic3.WidthScale = 100;
pic3.HeightScale = 100;

//Save the workbook in excel format
wb.Save(“ClayShannon.xlsx”);