Page 1 of 1

Resize image to bounding box and maintain aspect

Posted: Fri Feb 10, 2012 12:18 am
by blewis
Perhaps I am just not reading the docs enough, but I am having trouble finding a solution to my resize problem. I have to resize a set of images to fit within a specific set of dimensions (a bounding box). I don't which axis might need to be adjusted ahead of time (height or width). So, for example, if the bounding area is 100x100 and I give it an image of 200x100, then the resized result would be 100x50. The straight Resize method doesn't maintain aspect ratios. The ResizeHeightRatio and ResizeWidthRatio methods assume you know ahead of time which axis needs adjustment. The CreateThumbnailHQ method seems to be a bit closer, but then appears to fill the background with a color instead of just keeping the aspect ratio.

Is there a single method I can use to solve this problem? Or do I need to do the math myself ahead of time and then just call Resize?

Thanks,
Bryan

Re: Resize image to bounding box and maintain aspect

Posted: Wed Feb 15, 2012 6:07 pm
by greenware
Just calculate the ratios of the bounding box and the image to determine which method to call.

' Bounding Box
h1 = 100
w1 = 100

'Image
h2 = 200
w2 = 100

r1 = h2/h1 ' = 2
r2 = w2/w1 ' = 1

if r1 >= r2 then
'use ResizeHeightRatio
else
'use ResizeWidthRatio
end if