private Rectangle GetImageRectangle(int targetW, int targetH, int w, int h, bool swapped)
{
if(w == h) {
// Square
return new Rectangle(0, 0, targetW, targetH);
}
else if(w > h) {
// Landscape
float scale = (float)targetW / (float)w;
if(targetW > w) scale = 1;
int width = (int)(w * scale);
int height = (int)(h * scale);
if(swapped) {
int temp = width;
width = height;
height = temp;
}
return new Rectangle((targetW - width) / 2, (targetH - height) / 2, width, height);
}
else {
// Portrait
float scale = (float)targetH / (float)h;
if(targetH > h) scale = 1;
int width = (int)(w * scale);
int height = (int)(h * scale);
if(swapped) {
int temp = width;
width = height;
height = temp;
}
return new Rectangle((targetW - width) / 2, (targetH - height) / 2, width, height);
}
}