Add canvas margin from percentage canvas size

This commit is contained in:
Leondro Lio 2020-10-22 16:05:23 +01:00
parent 3fae47ed53
commit 18818efd0a

View file

@ -24,6 +24,7 @@
#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)
{