Added random position option to image watermark

This commit is contained in:
Jaex 2019-04-01 15:05:52 +03:00
parent 2d20f36440
commit 430e9dab55

View file

@ -34,17 +34,17 @@ namespace ShareX.ImageEffectsLib
[Description("Image watermark")]
public class DrawImage : ImageEffect
{
[DefaultValue(""), Editor(typeof(ImageFileNameEditor), typeof(UITypeEditor))]
public string ImageLocation { get; set; }
[DefaultValue(ContentAlignment.BottomRight)]
public ContentAlignment Placement { get; set; }
[DefaultValue(typeof(Point), "5, 5")]
public Point Offset { get; set; }
[DefaultValue(true), Description("If image watermark size bigger than source image then don't draw it.")]
public bool AutoHide { get; set; }
[DefaultValue(""), Editor(typeof(ImageFileNameEditor), typeof(UITypeEditor))]
public string ImageLocation { get; set; }
[DefaultValue(false)]
public bool RandomPosition { get; set; }
[DefaultValue(DrawImageSizeMode.DontResize), Description("How the image watermark should be rescaled, if at all.")]
public DrawImageSizeMode SizeMode { get; set; }
@ -52,6 +52,9 @@ public class DrawImage : ImageEffect
[DefaultValue(typeof(Size), "0, 0")]
public Size Size { get; set; }
[DefaultValue(true), Description("If image watermark size bigger than source image then don't draw it.")]
public bool AutoHide { get; set; }
public DrawImage()
{
this.ApplyDefaultPropertyValues();
@ -82,7 +85,28 @@ public override Image Apply(Image img)
}
// Place the image
Point imagePosition = Helpers.GetPosition(Placement, Offset, img.Size, imageSize);
Point imagePosition;
if (RandomPosition)
{
int x = 0;
if (img.Width - imageSize.Width > 0)
{
x = MathHelpers.Random(0, img.Width - imageSize.Width);
}
int y = 0;
if (img.Height - imageSize.Height > 0)
{
y = MathHelpers.Random(0, img.Height - imageSize.Height);
}
imagePosition = new Point(x, y);
}
else
{
imagePosition = Helpers.GetPosition(Placement, Offset, img.Size, imageSize);
}
Rectangle imageRectangle = new Rectangle(imagePosition, imageSize);
if (AutoHide && !new Rectangle(0, 0, img.Width, img.Height).Contains(imageRectangle))