ShareX/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs

128 lines
3.9 KiB
C#
Raw Permalink Normal View History

2020-07-16 06:16:43 +12:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2020-07-16 06:16:43 +12:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
2020-07-16 06:16:43 +12:00
namespace ShareX.ImageEffectsLib
{
2020-08-05 14:24:49 +12:00
[Description("Force proportions")]
2020-07-16 06:16:43 +12:00
internal class ForceProportions : ImageEffect
{
2020-08-05 14:24:49 +12:00
private int proportionalWidth = 1;
2020-07-16 06:16:43 +12:00
2020-08-05 14:24:49 +12:00
[DefaultValue(1)]
public int ProportionalWidth
2020-07-16 06:16:43 +12:00
{
2020-08-05 14:24:49 +12:00
get
{
return proportionalWidth;
2020-07-16 06:16:43 +12:00
}
set
{
2020-08-05 14:24:49 +12:00
proportionalWidth = Math.Max(1, value);
2020-07-16 06:16:43 +12:00
}
}
2020-08-05 14:24:49 +12:00
private int proportionalHeight = 1;
[DefaultValue(1)]
public int ProportionalHeight
2020-07-16 06:16:43 +12:00
{
get
{
2020-08-05 14:24:49 +12:00
return proportionalHeight;
2020-07-16 06:16:43 +12:00
}
set
{
2020-08-05 14:24:49 +12:00
proportionalHeight = Math.Max(1, value);
2020-07-16 06:16:43 +12:00
}
}
2020-08-05 14:24:49 +12:00
public enum ForceProportionsMethod
2020-07-16 06:16:43 +12:00
{
Grow,
Crop
}
2020-08-05 14:24:49 +12:00
[DefaultValue(ForceProportionsMethod.Grow)]
public ForceProportionsMethod Method { get; set; } = ForceProportionsMethod.Grow;
2020-07-16 06:16:43 +12:00
[DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
2020-08-05 14:24:49 +12:00
public Color GrowFillColor { get; set; } = Color.Transparent;
2020-07-16 06:16:43 +12:00
public override Bitmap Apply(Bitmap bmp)
{
2020-08-05 14:24:49 +12:00
float currentRatio = bmp.Width / (float)bmp.Height;
float targetRatio = proportionalWidth / (float)proportionalHeight;
2020-08-05 14:24:49 +12:00
bool isTargetWider = targetRatio > currentRatio;
2020-08-05 14:24:49 +12:00
int targetWidth = bmp.Width;
int targetHeight = bmp.Height;
int marginLeft = 0;
int marginTop = 0;
2020-07-16 06:16:43 +12:00
2020-08-05 14:24:49 +12:00
if (Method == ForceProportionsMethod.Crop)
2020-07-16 06:16:43 +12:00
{
2020-08-05 14:24:49 +12:00
if (isTargetWider)
{
2020-08-05 14:24:49 +12:00
targetHeight = (int)Math.Round(bmp.Width / targetRatio);
marginTop = (bmp.Height - targetHeight) / 2;
}
2020-07-16 06:16:43 +12:00
else
{
2020-08-05 14:24:49 +12:00
targetWidth = (int)Math.Round(bmp.Height * targetRatio);
marginLeft = (bmp.Width - targetWidth) / 2;
}
2020-08-05 14:24:49 +12:00
return ImageHelpers.CropBitmap(bmp, new Rectangle(marginLeft, marginTop, targetWidth, targetHeight));
2020-07-16 06:16:43 +12:00
}
2020-08-05 14:24:49 +12:00
else if (Method == ForceProportionsMethod.Grow)
2020-07-16 06:16:43 +12:00
{
2020-08-05 14:24:49 +12:00
if (isTargetWider)
{
2020-08-05 14:24:49 +12:00
targetWidth = (int)Math.Round(bmp.Height * targetRatio);
}
2020-07-16 06:16:43 +12:00
else
{
2020-08-05 14:24:49 +12:00
targetHeight = (int)Math.Round(bmp.Width / targetRatio);
}
2020-08-05 14:24:49 +12:00
return ImageHelpers.ResizeImage(bmp, targetWidth, targetHeight, false, true, GrowFillColor);
2020-07-16 06:16:43 +12:00
}
return bmp;
}
protected override string GetSummary()
{
return $"{ProportionalWidth}, {ProportionalHeight}";
}
2020-07-16 06:16:43 +12:00
}
}