Resize image to bounding box and maintain aspect

Discussions about image processing and document imaging.
Post Reply
blewis
Posts: 38
Joined: Wed May 18, 2011 4:28 am

Resize image to bounding box and maintain aspect

Post by blewis » Fri Feb 10, 2012 12:18 am

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

greenware
Posts: 16
Joined: Tue Jan 10, 2012 7:15 pm

Re: Resize image to bounding box and maintain aspect

Post by greenware » Wed Feb 15, 2012 6:07 pm

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

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests