I am trying to resize an image by keeping the aspect ratio. It should be just large enough to fill the screen with no blank space and if necessary some of the image should be off-screen.
The image below shows how the yellow image should be sized based on the black screen size.
Heres the Code that I am actually using, is there any better way to do this?
if(bwidth > bheight) {
if(bwidth > swidth && bheight > sheight) {
new_height = sheight;
new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));
} else if(bwidth > swidth && bheight < sheight) {
new_height = sheight;
new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));
} else if(bwidth < swidth && bheight < sheight) {
new_height = sheight;
new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));
} else if(bwidth < swidth && bheight > sheight) {
new_height = sheight;
new_width = (int) ((double) (bwidth/100)*(sheight/((double) (bheight)/100)));
} else if(bwidth >= swidth && bheight >= sheight) {
new_width = swidth;
new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));
}
} else if(bwidth < bheight) {
if(bwidth > swidth && bheight > sheight) {
new_width = swidth;
new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));
} else if(bwidth < swidth && bheight > sheight) {
new_width = swidth;
new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));
} else if(bwidth < swidth && bheight < sheight) {
new_width = swidth;
new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));
} else if(bwidth < swidth && bheight < sheight) {
new_width = swidth;
new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));
} else if(bwidth >= swidth && bheight >= sheight) {
new_width = swidth;
new_height = (int) ((double) (bheight/100)*(swidth/((double) (bwidth)/100)));
}
}
- swidth = screen width
- sheight = screen height
- bwidth = image width
- bheight = image height