Merge pull request #5146 from 7coil/master

Add canvas margin from percentage canvas size
This commit is contained in:
Jaex 2020-10-23 04:50:05 +03:00 committed by GitHub
commit b47687f9fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -24,6 +24,7 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
@ -36,6 +37,9 @@ internal class Canvas : ImageEffect
[DefaultValue(typeof(Padding), "0, 0, 0, 0")]
public Padding Margin { get; set; }
[DefaultValue(CanvasMarginMode.AbsoluteSize), Description("How the margin around the canvas will be calculated."), TypeConverter(typeof(EnumDescriptionConverter))]
public CanvasMarginMode MarginMode { get; set; }
[DefaultValue(typeof(Color), "Transparent"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color Color { get; set; }
@ -44,9 +48,32 @@ public Canvas()
this.ApplyDefaultPropertyValues();
}
public enum CanvasMarginMode
{
AbsoluteSize,
PercentageOfCanvas
}
public override Bitmap Apply(Bitmap bmp)
{
Bitmap bmpResult = ImageHelpers.AddCanvas(bmp, Margin, Color);
Padding canvasMargin;
if (MarginMode == CanvasMarginMode.PercentageOfCanvas)
{
// Calculate the amount of padding to add to the sides, based on canvas size.
canvasMargin = new Padding();
canvasMargin.Top = (int)Math.Round(Margin.Top / 100f * bmp.Height);
canvasMargin.Bottom = (int)Math.Round(Margin.Bottom / 100f * bmp.Height);
canvasMargin.Left = (int)Math.Round(Margin.Left / 100f * bmp.Width);
canvasMargin.Right = (int)Math.Round(Margin.Right / 100f * bmp.Width);
}
else
{
// Use the margin as is (absolute size)
canvasMargin = Margin;
}
Bitmap bmpResult = ImageHelpers.AddCanvas(bmp, canvasMargin, Color);
if (bmpResult == null)
{