java - resizing an image without compromising the aspect ratio -


i use following codes resize image problem don't maintain original aspect ratio, there way can resize image without compromising aspect ratio.

bufferedimage img1 = new bufferedimage(800, 600,                     bufferedimage.type_int_rgb);             img1.creategraphics()                     .drawimage(                             imageio.read(                                     new file("/home/rvkydmpo/webapps/root/images/profilepicture/"                                             + filename)).getscaledinstance(800,                                     600, image.scale_smooth), 0, 0, null);             imageio.write(img1, "jpg", new file("/home/rvkydmpo/webapps/root/images/profilepicture/" +filename)); 

you might want read the docs of getscaledinstance():

image java.awt.image.getscaledinstance(int width, int height, int hints) 

creates scaled version of image. new image object returned render image @ specified width , height default. new image object may loaded asynchronously if original source image has been loaded completely.

if either width or height negative number value substituted maintain aspect ratio of original image dimensions. if both width , height negative, original image dimensions used.

(emphasis mine)


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -