Centering of images

Hi,


I'm trying to center an image in FlexCel and having trouble.  All to do with the weird and wonderful coordinate systems being used by Excel.

I've followed https://www.tmssoftware.com/site/forum/forum_posts.asp?TID=3763&title=inserting-images-in-sheets which was a great help to understanding more, but still I am struggling.

In my app, the customer can choose any image and I am trying to place it in a named range at the top of the page.  The issue is how to calculate the X and Y offset to where exactly to place the image, from the size of the picture chosen by the customer.  

I use GetColWidth() and GetRowHeight() to measure the total width and height of the range, and then working my way across the columns and rows to the location where the image should be placed.  That works.  However, in order to maintain the aspect ratio I need to calculate the aspect ratio of the named range which is difficult as one dimension is in characters and the other in points.

All of this leads me to believe I am trying too hard.  

Is there an easy way to center an image in FlexCel?

- Niels

There is no built in way (that I remember on top of my head)to do it with the api, but you can do it with reports. If yo look at the code FlexCelReport uses, you need to use TExcelMetrics.RowMult and ColMult to convert from excel! Internal units to points (1/72 of an inch)



Here is the relevant code from FlexCelReport:



   TImageHAlign.Center:

    begin

      begin

        cw := ev.DataFile.GetColWidth(ImgProps.Anchor.Col1) / TExcelMetrics.ColMult(ev.DataFile);

        Dx1Pix := Convert.ToInt32(((cw - ev.Width) / 2) + ColOffs);

      end;

    end;



And it is similar for rows. Once you have e erything in points, it is simple to center the image and keep the ratio.



I am typing this from a tablet and it is no fun, so sorry I can't send a more complete code example. If you still need it, I can send it when I come back home in about a week. But rowmult/colmult should be all you need. It is what flexcel uses everywhere to render the files to pdf, html or images

Aha!  Thanks for that, and sorry to force you to support me on a tablet! :-)


I'll have a look at RowMult and ColMult.