public Size SizeToRatio(Size size, bool expand)
{
double ratio = AspectRatio;
double height = size.Width/ratio;
double width = size.Height*ratio;
if (expand) {
width = Math.Max(width, size.Width);
height = Math.Max(height, size.Height);
} else {
width = Math.Min(width, size.Width);
height = Math.Min(height, size.Height);
}
return new Size(width, height);
}