public static System.Drawing.Size GetImageSize(System.Drawing.Size currentSize, System.Drawing.Size maxSize, bool scaleUp)
{
System.Drawing.Size newSize = currentSize;
double ratioX = Convert.ToDouble(maxSize.Width) / Convert.ToDouble(currentSize.Width);
double ratioY = Convert.ToDouble(maxSize.Height) / Convert.ToDouble(currentSize.Height);
double ratio = System.Math.Min(ratioX, ratioY);
if (ratio < 1.0 || scaleUp)
{
newSize.Width = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Width) * ratio), 0);
newSize.Height = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Height) * ratio), 0);
}
return newSize;
}
public static System.Drawing.Size GetMinImageSizeToFit(System.Drawing.Size currentSize, System.Drawing.Size targetSize)
{
System.Drawing.Size newSize = currentSize;
double ratioX = Convert.ToDouble(targetSize.Width) / Convert.ToDouble(currentSize.Width);
double ratioY = Convert.ToDouble(targetSize.Height) / Convert.ToDouble(currentSize.Height);
double ratio = System.Math.Max(ratioX, ratioY);
newSize.Width = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Width) * ratio), 0);
newSize.Height = DoubleToIntDef(Math.Floor(Convert.ToDouble(currentSize.Height) * ratio), 0);
return newSize;
}
private static int DoubleToIntDef(double d, int def)
{
try
{
return Convert.ToInt32(d);
}
catch (Exception)
{
return def;
}
}
12200cookie-checkC# Calculate image size for fitting or cropping