Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Mon Jul 29, 2024 1:56 pm

Hi, I'm using Spire.xls 14.7.2, I have some big images (around 3000x2500) that I want to place inside a range without changing the actual proportions of the image, I tried with this method:
Code: Select all
sheet.Pictures.Add(27, 1, 45, 48, myImage);

But this stretches the image to fill the space, is there a method to lock the proportions of the image and just scale it down to fit inside the range that I select?
In this case I'm using a range of a lot of cells for the example, but I could merge the cells to make it one big cell if this changes something.
This is what happens, I want it to fit insted I get a stretch:
Image

SamueleSV
 
Posts: 4
Joined: Tue Mar 19, 2024 2:01 pm

Tue Jul 30, 2024 9:42 am

Hello,

Thanks for your inquiry.
You can refer to the following example code to insert your image. If there is any other issues, just feel free to write back.
Code: Select all
  Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];
            //Define the location
            int fr = 27;
            int to = 45;
            //Calculate the height of the target area in point
            int hg = CalculatingHeight(sheet, fr, to);
            String file = "picPath";
            //Loading images
            System.Drawing.Image img = System.Drawing.Image.FromFile(file);
            //Original image height in pixel
            int imh = img.Height;
            //Converts the image's pixel to point
            float imghP = imh * 72 / 96;
            //Calculate the scale
            float scal = hg / imghP * 100;
            //Inserts the image into the specified area
            ExcelPicture picture = sheet.Pictures.Add(fr, 1, file, (int)Math.Round(scal), (int)Math.Round(scal));
            picture.IsLockAspectRatio = true;
            workbook.SaveToFile(@"out.xlsx", ExcelVersion.Version2010);
        }
        static int CalculatingHeight(Worksheet sheet,int fr, int to)
        {           
            int height = 0;
            for (int i=fr; i<= to; i++)
            {
                height = height + (int)sheet.Rows[fr].RowHeight;
             }
            return height;
        }


Sincerely,
Amin
E-iceblue support team
User avatar

Amin.Gan
 
Posts: 283
Joined: Mon Jul 15, 2024 5:40 am

Wed Jul 31, 2024 2:09 pm

Thanks for the answer, I had to modify the code a bit but it was close to what I was trying to do, but this materialized another problem, the code you provided helped me scale the image, but now that the image doesn't cover the entire cell space I need to find a way to center the image in the cell, is there a way to work with the previous code and center the image horizontally in a merged cell?
This is your code modified for my interest:
Code: Select all
float hg;
float imh;

//Calculate the height of the target area in point
hg = CalculatingHeight(sheet, firstRow, lastRow);

//Original image height in pixel
imh = img.Height;

//Converts the image's pixel lastRow point
float imghP = imh * 72 / 96;
//Calculate the scale
float scal = hg / imghP * 100;
//Inserts the image into the specified area
ExcelPicture picture = sheet.Pictures.Add(firstRow, 1, img);

sheet.Pictures.Last().Scale((int)scal, (int)scal);

picture.IsLockAspectRatio = true;


Since this doesn't let you scale the image precisely with decimals I added this piece to reduced unused space:
Code: Select all
double frac = scal % 1;

double toAddLater = (picture.Height / scal) * frac;

picture.Height += (int)toAddLater;

SamueleSV
 
Posts: 4
Joined: Tue Mar 19, 2024 2:01 pm

Thu Aug 01, 2024 8:01 am

Hi,

Thank you for your feedback..
I have adjusted the example code based on your description. Attached files are my test document and results for your reference. If you have any other questions, please provide us with your testing documents and the desired result documents for further investigation.
Code: Select all
  Workbook workbook = new Workbook();
           workbook.LoadFromFile(@"in.xlsx");
            Worksheet sheet = workbook.Worksheets[0];
            //Loading image
            String file = @"logo.png";
            System.Drawing.Image img = System.Drawing.Image.FromFile(file);
            int imh = img.Height;
            int imw = img.Width;
            //Calculate the original scale of the image
            float imgScale = (float)imh / imw;
            //Inserts the image into the specified area
            ExcelPicture picture = sheet.Pictures.Add(27, 1, 45, 48, file);
            //ExcelPicture filled width
            int fillWidth = picture.Width;
            //Calculate the width of the image
            picture.Width = (int)(picture.Height / imgScale);
            int imgw = picture.Width;
            //Center the picture
            int moveRight = (fillWidth - imgw) / 2;
            picture.Left = moveRight;
            picture.IsLockAspectRatio = true;
            workbook.SaveToFile(@"out.xlsx", ExcelVersion.Version2010);


Sincerely,
Amin
E-iceblue support team
Attachments
test-file.rar
(1.07 MiB) Downloaded 326 times
User avatar

Amin.Gan
 
Posts: 283
Joined: Mon Jul 15, 2024 5:40 am

Return to Spire.XLS