mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-30 01:07:21 +13:00
Initial work for random images effect
This commit is contained in:
parent
f5e5c5c5b4
commit
8e800ff315
5 changed files with 159 additions and 44 deletions
|
@ -1502,5 +1502,16 @@ public static string XMLFormat(string xml)
|
|||
return sReader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<string> GetFilesByExtensions(string directoryPath, params string[] extensions)
|
||||
{
|
||||
return GetFilesByExtensions(new DirectoryInfo(directoryPath), extensions);
|
||||
}
|
||||
|
||||
public static IEnumerable<string> GetFilesByExtensions(DirectoryInfo directoryInfo, params string[] extensions)
|
||||
{
|
||||
HashSet<string> allowedExtensions = new HashSet<string>(extensions, StringComparer.OrdinalIgnoreCase);
|
||||
return directoryInfo.EnumerateFiles().Where(f => allowedExtensions.Contains(f.Extension)).Select(x => x.FullName);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -66,58 +66,61 @@ public override Image Apply(Image img)
|
|||
{
|
||||
using (Image img2 = ImageHelpers.LoadImage(ImageLocation))
|
||||
{
|
||||
// Calculate size first
|
||||
Size imageSize = img2.Size;
|
||||
if (SizeMode == DrawImageSizeMode.AbsoluteSize)
|
||||
if (img2 != null)
|
||||
{
|
||||
// Use Size property
|
||||
imageSize = Size;
|
||||
}
|
||||
else if (SizeMode == DrawImageSizeMode.PercentageOfWatermark)
|
||||
{
|
||||
// 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)
|
||||
imageSize = new Size((int)(img.Width * (Size.Width / 100.0)), (int)(img.Height * (Size.Height / 100.0)));
|
||||
}
|
||||
|
||||
// Place the image
|
||||
Point imagePosition;
|
||||
if (RandomPosition)
|
||||
{
|
||||
int x = 0;
|
||||
if (img.Width - imageSize.Width > 0)
|
||||
// Calculate size first
|
||||
Size imageSize = img2.Size;
|
||||
if (SizeMode == DrawImageSizeMode.AbsoluteSize)
|
||||
{
|
||||
x = MathHelpers.Random(0, img.Width - imageSize.Width);
|
||||
// Use Size property
|
||||
imageSize = Size;
|
||||
}
|
||||
else if (SizeMode == DrawImageSizeMode.PercentageOfWatermark)
|
||||
{
|
||||
// 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)
|
||||
imageSize = new Size((int)(img.Width * (Size.Width / 100.0)), (int)(img.Height * (Size.Height / 100.0)));
|
||||
}
|
||||
|
||||
int y = 0;
|
||||
if (img.Height - imageSize.Height > 0)
|
||||
// Place the image
|
||||
Point imagePosition;
|
||||
if (RandomPosition)
|
||||
{
|
||||
y = MathHelpers.Random(0, img.Height - imageSize.Height);
|
||||
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);
|
||||
}
|
||||
|
||||
imagePosition = new Point(x, y);
|
||||
}
|
||||
else
|
||||
{
|
||||
imagePosition = Helpers.GetPosition(Placement, Offset, img.Size, imageSize);
|
||||
}
|
||||
Rectangle imageRectangle = new Rectangle(imagePosition, imageSize);
|
||||
|
||||
Rectangle imageRectangle = new Rectangle(imagePosition, imageSize);
|
||||
if (AutoHide && !new Rectangle(0, 0, img.Width, img.Height).Contains(imageRectangle))
|
||||
{
|
||||
return img;
|
||||
}
|
||||
|
||||
if (AutoHide && !new Rectangle(0, 0, img.Width, img.Height).Contains(imageRectangle))
|
||||
{
|
||||
return img;
|
||||
}
|
||||
|
||||
using (Graphics g = Graphics.FromImage(img))
|
||||
{
|
||||
g.SetHighQuality();
|
||||
g.DrawImage(img2, imageRectangle);
|
||||
using (Graphics g = Graphics.FromImage(img))
|
||||
{
|
||||
g.SetHighQuality();
|
||||
g.DrawImage(img2, imageRectangle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
99
ShareX.ImageEffectsLib/Drawings/DrawRandomImages.cs
Normal file
99
ShareX.ImageEffectsLib/Drawings/DrawRandomImages.cs
Normal file
|
@ -0,0 +1,99 @@
|
|||
#region License Information (GPL v3)
|
||||
|
||||
/*
|
||||
ShareX - A program that allows you to take screenshots and share any file type
|
||||
Copyright (c) 2007-2019 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#endregion License Information (GPL v3)
|
||||
|
||||
using ShareX.HelpersLib;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Design;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace ShareX.ImageEffectsLib
|
||||
{
|
||||
[Description("Random images")]
|
||||
public class DrawRandomImages : ImageEffect
|
||||
{
|
||||
[DefaultValue(""), Editor(typeof(DirectoryNameEditor), typeof(UITypeEditor))]
|
||||
public string ImageFolder { get; set; }
|
||||
|
||||
private int imageCount;
|
||||
|
||||
[DefaultValue(1)]
|
||||
public int ImageCount
|
||||
{
|
||||
get
|
||||
{
|
||||
return imageCount;
|
||||
}
|
||||
set
|
||||
{
|
||||
imageCount = value.Clamp(1, 100);
|
||||
}
|
||||
}
|
||||
|
||||
public DrawRandomImages()
|
||||
{
|
||||
this.ApplyDefaultPropertyValues();
|
||||
}
|
||||
|
||||
public override Image Apply(Image img)
|
||||
{
|
||||
string imageFolder = Helpers.ExpandFolderVariables(ImageFolder);
|
||||
|
||||
if (!string.IsNullOrEmpty(imageFolder) && Directory.Exists(imageFolder))
|
||||
{
|
||||
string[] files = Helpers.GetFilesByExtensions(imageFolder, ".png", ".jpg").ToArray();
|
||||
|
||||
if (files.Length > 0)
|
||||
{
|
||||
using (Graphics g = Graphics.FromImage(img))
|
||||
{
|
||||
g.SetHighQuality();
|
||||
|
||||
for (int i = 0; i < ImageCount; i++)
|
||||
{
|
||||
string randomFile = MathHelpers.RandomPick(files);
|
||||
|
||||
using (Image img2 = ImageHelpers.LoadImage(randomFile))
|
||||
{
|
||||
if (img2 != null)
|
||||
{
|
||||
int widthOffset = img.Width - img2.Width - 1;
|
||||
int heightOffset = img.Height - img2.Height - 1;
|
||||
Rectangle rect = new Rectangle(MathHelpers.Random(Math.Min(0, widthOffset), Math.Max(0, widthOffset)),
|
||||
MathHelpers.Random(Math.Min(0, heightOffset), Math.Max(0, heightOffset)), img2.Width, img2.Height);
|
||||
g.DrawImage(img2, rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -99,7 +99,8 @@ private void AddAllEffectsToContextMenu()
|
|||
typeof(DrawBorder),
|
||||
typeof(DrawCheckerboard),
|
||||
typeof(DrawImage),
|
||||
typeof(DrawText));
|
||||
typeof(DrawText),
|
||||
typeof(DrawRandomImages));
|
||||
|
||||
AddEffectToContextMenu(Resources.ImageEffectsForm_AddAllEffectsToTreeView_Manipulations,
|
||||
typeof(AutoCrop),
|
||||
|
|
|
@ -109,6 +109,7 @@
|
|||
<Compile Include="Adjustments\MatrixColor.cs" />
|
||||
<Compile Include="Drawings\DrawCheckerboard.cs" />
|
||||
<Compile Include="Drawings\DrawImage.cs" />
|
||||
<Compile Include="Drawings\DrawRandomImages.cs" />
|
||||
<Compile Include="Drawings\DrawText.cs" />
|
||||
<Compile Include="Enums.cs" />
|
||||
<Compile Include="Filters\EdgeDetect.cs" />
|
||||
|
|
Loading…
Reference in a new issue