469,953 Members | 2,403 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

Image scaling by Width & Height constant - preserve aspect ratio

Hi,

I'm looking for an algorithm to resize an image of arbitrary size to a
"fixed" / required width and height.

For example, my application requires that images be no larger than 440 pixel
(height) x 780 (wide)

So the "preferred size" would be 440 x 780

If an image is too large, either by width or height it should be resized
until less than or equal to 440 x 780 and preserve aspect ratio.

Thanks for any tips.

Farce
Jul 17 '06 #1
2 19094
Farce Milverk wrote:
Hi,

I'm looking for an algorithm to resize an image of arbitrary size to a
"fixed" / required width and height.

For example, my application requires that images be no larger than 440 pixel
(height) x 780 (wide)

So the "preferred size" would be 440 x 780

If an image is too large, either by width or height it should be resized
until less than or equal to 440 x 780 and preserve aspect ratio.

Thanks for any tips.

Farce

Ok,
if the ratio is lower than 780/440 (your fixed width split on the fixed
height), then you need to resize by height, if it is (equal to
or(equality can be put in either one of the two methods)) higher than
this one, then it should be resized by width...
So:

double fixedRatio = 780.0/440.0;
if((double)image.getWidth()/image.getHeight >= fixedRatio)
{
resizeImage( image, 780, image.getHeight()/(image.getWidth()/780.0) );
// This requires that there exists a resizeImage method, but I don't
know what you want, drawImage or if you are making a file or what, so
this is kind of pseudo-code.
}
else
{
resizeImage( image, image.getWidth()/(image.getHeight()/440.0), 440);
}

I think that should do it, but I'm kind of tired right now...
Jul 17 '06 #2
Hi,
I want to print the screen ie(768*1024).For this I have capture the screen in to a BufferedImage.Now when I try to print it doesnt print completely.So please guide me how do I scale it and print.
My code :
Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSiz e());
Graphics2D g2d= (Graphics2D)g;
BufferedImage img= robot.createScreenCapture(area);
g2d.drawImage(img,0,0,null);

Latheef
Jul 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Stephane D'Alu | last post: by
1 post views Thread by Abs | last post: by
2 posts views Thread by Carl Gilbert | last post: by
3 posts views Thread by Stimp | last post: by
9 posts views Thread by Maxi | last post: by
reply views Thread by mharness | last post: by
9 posts views Thread by tshad | last post: by
3 posts views Thread by Danny Ni | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.