Use ResizeBitmap for ForceProportions effect

Also some refactoring
This commit is contained in:
L1Q 2020-07-15 21:09:26 +03:00
parent dc10a837cf
commit 2039c99909

View file

@ -36,7 +36,7 @@ internal class ForceProportions : ImageEffect
private int width = 1; private int width = 1;
private int height = 1; private int height = 1;
//[DefaultValue(typeof(Padding), "0, 0, 0, 0")] [DefaultValue(typeof(int), "1")]
public int Width public int Width
{ {
get { get {
@ -47,6 +47,7 @@ public int Width
width = Math.Max(1, value); width = Math.Max(1, value);
} }
} }
[DefaultValue(typeof(int), "1")]
public int Height public int Height
{ {
get get
@ -76,18 +77,14 @@ public ForceProportions()
public override Bitmap Apply(Bitmap bmp) public override Bitmap Apply(Bitmap bmp)
{ {
// NOTE: I don't know if input bitmap can have 0 as height
// TODO: remove this if it can't
if (bmp.Width < 1 || bmp.Height < 1)
return bmp;
float target_ratio = (float)width / (float)height;
float current_ratio = (float)bmp.Width / (float)bmp.Height; float current_ratio = (float)bmp.Width / (float)bmp.Height;
float target_ratio = (float)width / (float)height;
bool is_target_wider = target_ratio > current_ratio;
int target_width = bmp.Width; int target_width = bmp.Width;
int target_height = bmp.Height; int target_height = bmp.Height;
bool is_target_wider = target_ratio > current_ratio; if (Method == ForceMethod.Crop)
if(Method == ForceMethod.Crop)
{ {
if (is_target_wider) if (is_target_wider)
target_height = (int)Math.Round(bmp.Width / target_ratio); target_height = (int)Math.Round(bmp.Width / target_ratio);
@ -101,28 +98,13 @@ public override Bitmap Apply(Bitmap bmp)
: 0; : 0;
return ImageHelpers.CropBitmap(bmp, new Rectangle(margin_left, margin_top, target_width, target_height)); return ImageHelpers.CropBitmap(bmp, new Rectangle(margin_left, margin_top, target_width, target_height));
} }
if(Method == ForceMethod.Grow) if (Method == ForceMethod.Grow)
{ {
if (is_target_wider) if (is_target_wider)
target_width = (int)Math.Round(bmp.Height * target_ratio); target_width = (int)Math.Round(bmp.Height * target_ratio);
else else
target_height = (int)Math.Round(bmp.Width / target_ratio); target_height = (int)Math.Round(bmp.Width / target_ratio);
int margin_hor = is_target_wider return ImageHelpers.ResizeImage(bmp, target_width, target_height, false, true, GrowFillColor);
? (target_width - bmp.Width) / 2
: 0;
int margin_ver = is_target_wider
? 0
: (target_height - bmp.Height) / 2;
Bitmap bmpResult = ImageHelpers.AddCanvas(bmp, new Padding(margin_hor, margin_ver, margin_hor, margin_ver), GrowFillColor);
if (bmpResult == null)
{
return bmp;
}
bmp.Dispose();
return bmpResult;
} }
return bmp; return bmp;