Page 1 of 1

Keep Image Aspect Ratio

Posted: Sat May 03, 2014 11:52 am
by andrewferguson
Hello,
I have a stack that uses an image object. The user is able to select an image from a list, and the image is loaded up into the image control through the fileName property.
However, when the fileName is set the the image path, the size and width of the image control change, causing it to cover up objects on the card. To solve this I locked the image control's width and height. Now when loaded, images appear stretched, as they are being set the the specified width and height.

What I want is the image to maintain its aspect ratio, without changing the size of the image control. This would mean that there would be blank spaces on the edges of the image control.

Does anyone know how to do this?

Thanks,
Andrew

Re: Keep Image Aspect Ratio

Posted: Sat May 03, 2014 12:10 pm
by Dixie
Hi...

Try this... I have attached a stack with an image
Import picure and then choose an image...
Use the '+' and '-' buttons to alter the size...
Is this something that might help ?

Re: Keep Image Aspect Ratio

Posted: Sat May 03, 2014 8:04 pm
by [-hh]
..........

Re: Keep Image Aspect Ratio

Posted: Sat May 03, 2014 8:19 pm
by Dixie
Hermann..

If you look closely at the stack I posted, you will see that I use the same method as you do... the image is grouped... As for a fast machine needed, I'm not too sure about that as I have to slow the repeat loop down..

Re: Keep Image Aspect Ratio

Posted: Sat May 03, 2014 9:20 pm
by [-hh]
..........

Re: Keep Image Aspect Ratio

Posted: Sat May 03, 2014 10:40 pm
by Simon
Hi Andrew,
I think what you are asking for is to solve when a user selects a portrait photo or landscape.

If so here is the celebrated code that Klaus came up with

Code: Select all

on mouseUp
   answer file "Please select an image"
   lock screen
   set the lockLocation of img "addImage"  to false
   set cursor to busy
   set the filename of img "addImage" to it
   resizePicture
end mouseUp

on resizePicture
   put the formattedwidth of img "addImage" into w
   put the formattedheight of img "addImage" into h
   put 240 into tW
   put 179 into tH
   put mk_makeratio(tW,tH,w,h) into new_imagesize
   put item 1 of new_imagesize into newW
   put item 2 of new_imagesize into newH
   set the width of img "addImage" to newW
   set the height of img "addImage" to newH
   set the loc of img "addImage" to 188,93
   set the lockLocation of img "addImage"  to true
   unlock screen
   set cursor to arrow
end resizePicture

function mk_makeratio tW,tH,w,h
put min (tW/w,tH/h) into tscaleFactor
return round(w * tscaleFactor) & "," & round(h * tscaleFactor)
end mk_makeratio
set your tW and tH and loc.

Simon