mirror of
https://github.com/ShareX/ShareX.git
synced 2024-07-01 04:30:49 +12:00
Added random position option to image watermark
This commit is contained in:
parent
2d20f36440
commit
430e9dab55
|
@ -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();
|
||||
|
@ -63,26 +66,47 @@ public override Image Apply(Image img)
|
|||
{
|
||||
using (Image img2 = ImageHelpers.LoadImage(ImageLocation))
|
||||
{
|
||||
//Calculate size first
|
||||
// Calculate size first
|
||||
Size imageSize = img2.Size;
|
||||
if (SizeMode == DrawImageSizeMode.AbsoluteSize)
|
||||
{
|
||||
//Use Size property
|
||||
// Use Size property
|
||||
imageSize = Size;
|
||||
}
|
||||
else if (SizeMode == DrawImageSizeMode.PercentageOfWatermark)
|
||||
{
|
||||
//Relative size (percentage of watermark)
|
||||
// Relative size (percentage of watermark)
|
||||
imageSize = new Size((int)(img2.Width * (Size.Width / 100.0)), (int)(img2.Height * (Size.Height / 100.0)));
|
||||
}
|
||||
else if (SizeMode == DrawImageSizeMode.PercentageOfCanvas)
|
||||
{
|
||||
//Relative size (percentage of image)
|
||||
// Relative size (percentage of image)
|
||||
imageSize = new Size((int)(img.Width * (Size.Width / 100.0)), (int)(img.Height * (Size.Height / 100.0)));
|
||||
}
|
||||
|
||||
//Place the image
|
||||
Point imagePosition = Helpers.GetPosition(Placement, Offset, img.Size, imageSize);
|
||||
// Place the image
|
||||
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))
|
||||
|
|
Loading…
Reference in a new issue