Search code examples
c#image-processingpixelimage-resizing

Recalculating pixel coordinates after making an image smaller but the same aspect ratio


If I have a PDF page that is 8.5in x 11in @ 300 DPI.
It ends up being 2550px x 3300px.

Lets say there are a few X and Y coordinates on the location of a text character from when it was still a PDF such as:
X: 1281.6 and Y: 1022.4
or
1281.6 from the left and 1022.4px from the top

Now if I convert the PDF to an image and I would want the image to be smaller such as 816px x 1056px. This would be same aspect ratio but at 96 dpi instead of 300.

I am trying to figure out what the calculation would be to convert the X and Y coordinates of the text character to their smaller versions.


Solution

  • Like a percentage:

    newx = oldx * (newwidth / oldwidth)
    newy = oldy * (newheight / oldheight)
    

    Note that if your widths/heights are ints, cast one of them to double - 1000/2000 is 0, which does rather screw up the maths :) (1000.0/2000 or 1000/2000.0 on the other hand is 0.5)