Files
Arne Moerman 0bf5277be0 initial commit
2024-12-15 18:39:34 +01:00

56 lines
1.6 KiB
C#

namespace ClosestResolution
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void TextChanged(object sender, EventArgs e)
{
//try get int from textbox
if (int.TryParse(txtX.Text, out int width) && int.TryParse(txtY.Text, out int height))
{
//get closest resolution
var closest = ClosestResolution(width, height);
//set label text
lblX.Text = closest.Width.ToString();
lblY.Text = closest.Height.ToString();
}
else
{
lblX.Text = "Error";
lblY.Text = "Error";
}
}
private Size ClosestResolution(int width, int height)
{
//always go down in size in only one dimension
if (width > height) //landscape
{
if ((width / 3) * 4 > height) //height needs to be kept
{
return new Size((height * 4) / 3, height);
}
else
{
return new Size(width, (width * 3) / 4);
}
}
else //portrait
{
if ((height / 3) * 4 < width) //width needs to be kept
{
return new Size(width, (width * 4) / 3);
}
else
{
return new Size((height * 3) / 4, height);
}
}
}
}
}