From dc10a837cfda046020f81a9c37d0c25ae72ed95a Mon Sep 17 00:00:00 2001
From: L1Q <0xL1Q@ex.ua>
Date: Wed, 15 Jul 2020 21:16:43 +0300
Subject: [PATCH 1/3] Add ForceProportions image effect
---
.../Forms/ImageEffectsForm.cs | 1 +
.../Manipulations/ForceProportions.cs | 131 ++++++++++++++++++
.../ShareX.ImageEffectsLib.csproj | 1 +
3 files changed, 133 insertions(+)
create mode 100644 ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs
diff --git a/ShareX.ImageEffectsLib/Forms/ImageEffectsForm.cs b/ShareX.ImageEffectsLib/Forms/ImageEffectsForm.cs
index e683ae2e9..037d5cbff 100644
--- a/ShareX.ImageEffectsLib/Forms/ImageEffectsForm.cs
+++ b/ShareX.ImageEffectsLib/Forms/ImageEffectsForm.cs
@@ -129,6 +129,7 @@ private void AddAllEffectsToContextMenu()
typeof(Canvas),
typeof(Crop),
typeof(Flip),
+ typeof(ForceProportions),
typeof(Resize),
typeof(Rotate),
typeof(RoundedCorners),
diff --git a/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs b/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs
new file mode 100644
index 000000000..551c2e482
--- /dev/null
+++ b/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs
@@ -0,0 +1,131 @@
+#region License Information (GPL v3)
+
+/*
+ ShareX - A program that allows you to take screenshots and share any file type
+ Copyright (c) 2007-2020 ShareX Team
+
+ 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 .
+*/
+
+#endregion License Information (GPL v3)
+
+using ShareX.HelpersLib;
+using System;
+using System.ComponentModel;
+using System.Drawing;
+using System.Windows.Forms;
+
+namespace ShareX.ImageEffectsLib
+{
+ internal class ForceProportions : ImageEffect
+ {
+ private int width = 1;
+ private int height = 1;
+
+ //[DefaultValue(typeof(Padding), "0, 0, 0, 0")]
+ public int Width
+ {
+ get {
+ return width;
+ }
+ set
+ {
+ width = Math.Max(1, value);
+ }
+ }
+ public int Height
+ {
+ get
+ {
+ return height;
+ }
+ set
+ {
+ height = Math.Max(1, value);
+ }
+ }
+
+ public enum ForceMethod
+ {
+ Grow,
+ Crop
+ }
+
+ public ForceMethod Method { get; set; } = ForceMethod.Grow;
+
+ public Color GrowFillColor { get; set; } = Color.FromArgb(0);
+
+ public ForceProportions()
+ {
+ this.ApplyDefaultPropertyValues();
+ }
+
+ 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;
+ int target_width = bmp.Width;
+ int target_height = bmp.Height;
+
+ bool is_target_wider = target_ratio > current_ratio;
+ if(Method == ForceMethod.Crop)
+ {
+ if (is_target_wider)
+ target_height = (int)Math.Round(bmp.Width / target_ratio);
+ else
+ target_width = (int)Math.Round(bmp.Height * target_ratio);
+ int margin_left = is_target_wider
+ ? 0
+ : (bmp.Width - target_width) / 2;
+ int margin_top = is_target_wider
+ ? (bmp.Height - target_height) / 2
+ : 0;
+ return ImageHelpers.CropBitmap(bmp, new Rectangle(margin_left, margin_top, target_width, target_height));
+ }
+ if(Method == ForceMethod.Grow)
+ {
+ if (is_target_wider)
+ target_width = (int)Math.Round(bmp.Height * target_ratio);
+ else
+ target_height = (int)Math.Round(bmp.Width / target_ratio);
+ int margin_hor = is_target_wider
+ ? (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;
+ }
+ }
+}
\ No newline at end of file
diff --git a/ShareX.ImageEffectsLib/ShareX.ImageEffectsLib.csproj b/ShareX.ImageEffectsLib/ShareX.ImageEffectsLib.csproj
index f98cde0cd..c417f20b7 100644
--- a/ShareX.ImageEffectsLib/ShareX.ImageEffectsLib.csproj
+++ b/ShareX.ImageEffectsLib/ShareX.ImageEffectsLib.csproj
@@ -142,6 +142,7 @@
+
From 2039c999091d730093e84582eaffc0810e300ca9 Mon Sep 17 00:00:00 2001
From: L1Q <0xL1Q@ex.ua>
Date: Wed, 15 Jul 2020 21:09:26 +0300
Subject: [PATCH 2/3] Use ResizeBitmap for ForceProportions effect
Also some refactoring
---
.../Manipulations/ForceProportions.cs | 34 +++++--------------
1 file changed, 8 insertions(+), 26 deletions(-)
diff --git a/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs b/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs
index 551c2e482..cc5bfc139 100644
--- a/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs
+++ b/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs
@@ -36,7 +36,7 @@ internal class ForceProportions : ImageEffect
private int width = 1;
private int height = 1;
- //[DefaultValue(typeof(Padding), "0, 0, 0, 0")]
+ [DefaultValue(typeof(int), "1")]
public int Width
{
get {
@@ -47,6 +47,7 @@ public int Width
width = Math.Max(1, value);
}
}
+ [DefaultValue(typeof(int), "1")]
public int Height
{
get
@@ -76,18 +77,14 @@ public ForceProportions()
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 target_ratio = (float)width / (float)height;
+ bool is_target_wider = target_ratio > current_ratio;
+
int target_width = bmp.Width;
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)
target_height = (int)Math.Round(bmp.Width / target_ratio);
@@ -101,28 +98,13 @@ public override Bitmap Apply(Bitmap bmp)
: 0;
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)
target_width = (int)Math.Round(bmp.Height * target_ratio);
else
target_height = (int)Math.Round(bmp.Width / target_ratio);
- int margin_hor = is_target_wider
- ? (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 ImageHelpers.ResizeImage(bmp, target_width, target_height, false, true, GrowFillColor);
}
return bmp;
From 4b1a3d6229f2d45325a0a5919fa79b96e5c7b964 Mon Sep 17 00:00:00 2001
From: L1Q <0xL1Q@ex.ua>
Date: Wed, 5 Aug 2020 01:49:19 +0300
Subject: [PATCH 3/3] Update ForceProportions effect
- more consistent code formatting
- better color selection
---
.../Manipulations/ForceProportions.cs | 31 ++++++++++++-------
1 file changed, 20 insertions(+), 11 deletions(-)
diff --git a/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs b/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs
index cc5bfc139..aa6a588b1 100644
--- a/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs
+++ b/ShareX.ImageEffectsLib/Manipulations/ForceProportions.cs
@@ -27,17 +27,18 @@ You should have received a copy of the GNU General Public License
using System;
using System.ComponentModel;
using System.Drawing;
-using System.Windows.Forms;
+using System.Drawing.Design;
namespace ShareX.ImageEffectsLib
{
+ [Description("Force Proportions")]
internal class ForceProportions : ImageEffect
{
private int width = 1;
private int height = 1;
[DefaultValue(typeof(int), "1")]
- public int Width
+ public int ProportionalWidth
{
get {
return width;
@@ -48,7 +49,7 @@ public int Width
}
}
[DefaultValue(typeof(int), "1")]
- public int Height
+ public int ProportionalHeight
{
get
{
@@ -68,6 +69,7 @@ public enum ForceMethod
public ForceMethod Method { get; set; } = ForceMethod.Grow;
+ [DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color GrowFillColor { get; set; } = Color.FromArgb(0);
public ForceProportions()
@@ -77,33 +79,40 @@ public ForceProportions()
public override Bitmap Apply(Bitmap bmp)
{
- float current_ratio = (float)bmp.Width / (float)bmp.Height;
- float target_ratio = (float)width / (float)height;
+ float current_ratio = bmp.Width / (float)bmp.Height;
+ float target_ratio = width / (float)height;
+
bool is_target_wider = target_ratio > current_ratio;
int target_width = bmp.Width;
int target_height = bmp.Height;
+ int margin_left = 0;
+ int margin_top = 0;
if (Method == ForceMethod.Crop)
{
if (is_target_wider)
+ {
target_height = (int)Math.Round(bmp.Width / target_ratio);
+ margin_top = (bmp.Height - target_height) / 2;
+ }
else
+ {
target_width = (int)Math.Round(bmp.Height * target_ratio);
- int margin_left = is_target_wider
- ? 0
- : (bmp.Width - target_width) / 2;
- int margin_top = is_target_wider
- ? (bmp.Height - target_height) / 2
- : 0;
+ margin_left = (bmp.Width - target_width) / 2;
+ }
return ImageHelpers.CropBitmap(bmp, new Rectangle(margin_left, margin_top, target_width, target_height));
}
if (Method == ForceMethod.Grow)
{
if (is_target_wider)
+ {
target_width = (int)Math.Round(bmp.Height * target_ratio);
+ }
else
+ {
target_height = (int)Math.Round(bmp.Width / target_ratio);
+ }
return ImageHelpers.ResizeImage(bmp, target_width, target_height, false, true, GrowFillColor);
}