user547654
user547654

Reputation:

Image size calculation by keeping aspect ratio with different screen resolutions

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.

enter image description here

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)));

    }
}

Upvotes: 6

Views: 5356

Answers (2)

Erick Robertson
Erick Robertson

Reputation: 33082

Compare ratios.

If the width to height ratio of the image is more than the width to height ratio of the screen, then you know you'll be using the screen width and calculating the height. Otherwise you'll be using the screen height and calculating the width. Just make sure none of the heights are zero!

Note that the code here will resize the image so that it will always fill the screen. This effectively crops off any additional part of the image. To make the image as large as possible while being entirely visible, change the < to a > in the first line.

if (bwidth / bheight < swidth / sheight) {
    new_width = swidth;
    new_height = (int) Math.floor((double) bheight
                                  * (double) swidth / (double) bwidth);
} else {
    new_height = sheight;
    new_width = (int) Math.floor((double) bwidth
                                 * (double) sheight / (double) bheight);
}

I also made a couple more improvements:

  • Simplified the equations. Dividing a numerator and denominator both by 100 doesn't do anything.
  • Simplified the typecasting. I don't know the type of each variable, but they all need to be doubles.
  • Used Math.floor instead of just a typecast to int to make sure it doesn't round up.

Upvotes: 10

user547654
user547654

Reputation:

Well thank you @Erick Robertson Changed a litte bit, but now it works!

Here is the changed code:

if (bwidth / swidth <  bheight / sheight) {
    new_width = swidth;
    new_height = (int) Math.floor((double) bheight 
                                  * (double) swidth / (double) bwidth);
} else {
    new_height = sheight;
    new_width = (int) Math.floor((double) bwidth 
                                 * (double) sheight / (double) bheight);
}

Upvotes: -4

Related Questions