2019-12-08 20:23:20 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2020-02-05 20:19:48 +13:00
|
|
|
|
Copyright (c) 2007-2020 ShareX Team
|
2019-12-08 20:23:20 +13:00
|
|
|
|
|
|
|
|
|
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;
|
2019-12-16 01:39:15 +13:00
|
|
|
|
using System.Collections.Generic;
|
2019-12-08 20:23:20 +13:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Design;
|
2019-12-09 02:39:23 +13:00
|
|
|
|
using System.Drawing.Drawing2D;
|
2019-12-09 22:26:42 +13:00
|
|
|
|
using System.Drawing.Imaging;
|
2019-12-08 20:23:20 +13:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.ImageEffectsLib
|
|
|
|
|
{
|
2019-12-18 07:27:08 +13:00
|
|
|
|
[Description("Particles")]
|
|
|
|
|
public class DrawParticles : ImageEffect
|
2019-12-08 20:23:20 +13:00
|
|
|
|
{
|
|
|
|
|
[DefaultValue(""), Editor(typeof(DirectoryNameEditor), typeof(UITypeEditor))]
|
|
|
|
|
public string ImageFolder { get; set; }
|
|
|
|
|
|
|
|
|
|
private int imageCount;
|
|
|
|
|
|
|
|
|
|
[DefaultValue(1)]
|
|
|
|
|
public int ImageCount
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return imageCount;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2019-12-08 20:59:56 +13:00
|
|
|
|
imageCount = value.Clamp(1, 1000);
|
2019-12-08 20:23:20 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 01:31:23 +13:00
|
|
|
|
[DefaultValue(false)]
|
|
|
|
|
public bool RandomSize { get; set; }
|
|
|
|
|
|
|
|
|
|
[DefaultValue(64)]
|
|
|
|
|
public int RandomSizeMin { get; set; }
|
|
|
|
|
|
|
|
|
|
[DefaultValue(128)]
|
|
|
|
|
public int RandomSizeMax { get; set; }
|
|
|
|
|
|
2019-12-09 01:14:41 +13:00
|
|
|
|
[DefaultValue(false)]
|
2019-12-16 01:07:40 +13:00
|
|
|
|
public bool RandomAngle { get; set; }
|
2019-12-09 01:14:41 +13:00
|
|
|
|
|
2019-12-09 02:03:45 +13:00
|
|
|
|
[DefaultValue(0)]
|
2019-12-16 01:07:40 +13:00
|
|
|
|
public int RandomAngleMin { get; set; }
|
2019-12-09 02:03:45 +13:00
|
|
|
|
|
|
|
|
|
[DefaultValue(360)]
|
2019-12-16 01:07:40 +13:00
|
|
|
|
public int RandomAngleMax { get; set; }
|
2019-12-09 02:03:45 +13:00
|
|
|
|
|
2019-12-09 22:26:42 +13:00
|
|
|
|
[DefaultValue(false)]
|
|
|
|
|
public bool RandomOpacity { get; set; }
|
|
|
|
|
|
|
|
|
|
[DefaultValue(0)]
|
|
|
|
|
public int RandomOpacityMin { get; set; }
|
|
|
|
|
|
|
|
|
|
[DefaultValue(100)]
|
|
|
|
|
public int RandomOpacityMax { get; set; }
|
|
|
|
|
|
2019-12-16 01:39:15 +13:00
|
|
|
|
[DefaultValue(false)]
|
|
|
|
|
public bool NoOverlap { get; set; }
|
|
|
|
|
|
2019-12-16 03:54:23 +13:00
|
|
|
|
[DefaultValue(0)]
|
|
|
|
|
public int NoOverlapOffset { get; set; }
|
|
|
|
|
|
2019-12-16 01:39:15 +13:00
|
|
|
|
private List<Rectangle> imageRectangles = new List<Rectangle>();
|
|
|
|
|
|
2019-12-18 07:27:08 +13:00
|
|
|
|
public DrawParticles()
|
2019-12-08 20:23:20 +13:00
|
|
|
|
{
|
|
|
|
|
this.ApplyDefaultPropertyValues();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 10:16:55 +13:00
|
|
|
|
public override Bitmap Apply(Bitmap bmp)
|
2019-12-08 20:23:20 +13:00
|
|
|
|
{
|
2020-07-06 11:23:59 +12:00
|
|
|
|
string imageFolder = Helpers.ExpandFolderVariables(ImageFolder, true);
|
2019-12-08 20:23:20 +13:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(imageFolder) && Directory.Exists(imageFolder))
|
|
|
|
|
{
|
|
|
|
|
string[] files = Helpers.GetFilesByExtensions(imageFolder, ".png", ".jpg").ToArray();
|
|
|
|
|
|
|
|
|
|
if (files.Length > 0)
|
|
|
|
|
{
|
2019-12-16 01:39:15 +13:00
|
|
|
|
imageRectangles.Clear();
|
|
|
|
|
|
2020-03-22 10:16:55 +13:00
|
|
|
|
using (Graphics g = Graphics.FromImage(bmp))
|
2019-12-08 20:59:56 +13:00
|
|
|
|
using (ImageFilesCache imageCache = new ImageFilesCache())
|
2019-12-08 20:23:20 +13:00
|
|
|
|
{
|
2019-12-09 02:39:23 +13:00
|
|
|
|
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
2019-12-08 20:23:20 +13:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < ImageCount; i++)
|
|
|
|
|
{
|
2020-04-24 01:41:09 +12:00
|
|
|
|
string file = RandomFast.Pick(files);
|
2020-03-22 12:22:43 +13:00
|
|
|
|
Bitmap bmpCached = imageCache.GetImage(file);
|
2019-12-08 20:59:56 +13:00
|
|
|
|
|
2020-03-22 12:22:43 +13:00
|
|
|
|
if (bmpCached != null)
|
2019-12-08 20:23:20 +13:00
|
|
|
|
{
|
2020-03-22 12:22:43 +13:00
|
|
|
|
DrawImage(bmp, bmpCached, g);
|
2019-12-08 20:23:20 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-22 10:16:55 +13:00
|
|
|
|
return bmp;
|
2019-12-08 20:23:20 +13:00
|
|
|
|
}
|
2019-12-09 02:03:45 +13:00
|
|
|
|
|
|
|
|
|
private void DrawImage(Image img, Image img2, Graphics g)
|
|
|
|
|
{
|
|
|
|
|
int width, height;
|
|
|
|
|
|
|
|
|
|
if (RandomSize)
|
|
|
|
|
{
|
2020-04-24 01:41:09 +12:00
|
|
|
|
int size = RandomFast.Next(Math.Min(RandomSizeMin, RandomSizeMax), Math.Max(RandomSizeMin, RandomSizeMax));
|
2019-12-18 07:19:44 +13:00
|
|
|
|
width = size;
|
|
|
|
|
height = size;
|
|
|
|
|
|
|
|
|
|
if (img2.Width > img2.Height)
|
|
|
|
|
{
|
2019-12-23 00:50:06 +13:00
|
|
|
|
height = (int)Math.Round(size * ((double)img2.Height / img2.Width));
|
2019-12-18 07:19:44 +13:00
|
|
|
|
}
|
|
|
|
|
else if (img2.Width < img2.Height)
|
|
|
|
|
{
|
2019-12-23 00:50:06 +13:00
|
|
|
|
width = (int)Math.Round(size * ((double)img2.Width / img2.Height));
|
2019-12-18 07:19:44 +13:00
|
|
|
|
}
|
2019-12-09 02:03:45 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
width = img2.Width;
|
|
|
|
|
height = img2.Height;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 02:39:23 +13:00
|
|
|
|
if (width < 1 || height < 1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 02:59:44 +13:00
|
|
|
|
int xOffset = img.Width - width - 1;
|
|
|
|
|
int yOffset = img.Height - height - 1;
|
|
|
|
|
|
2019-12-16 03:54:23 +13:00
|
|
|
|
Rectangle rect, overlapRect;
|
2019-12-16 01:39:15 +13:00
|
|
|
|
int attemptCount = 0;
|
|
|
|
|
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
attemptCount++;
|
2019-12-16 03:54:23 +13:00
|
|
|
|
if (attemptCount > 1000)
|
2019-12-16 01:39:15 +13:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-12-16 03:54:23 +13:00
|
|
|
|
|
2020-04-24 01:41:09 +12:00
|
|
|
|
rect = new Rectangle(RandomFast.Next(Math.Min(0, xOffset), Math.Max(0, xOffset)),
|
|
|
|
|
RandomFast.Next(Math.Min(0, yOffset), Math.Max(0, yOffset)), width, height);
|
2019-12-16 03:54:23 +13:00
|
|
|
|
|
|
|
|
|
overlapRect = rect.Offset(NoOverlapOffset);
|
|
|
|
|
} while (NoOverlap && imageRectangles.Any(x => x.IntersectsWith(overlapRect)));
|
2019-12-16 01:39:15 +13:00
|
|
|
|
|
|
|
|
|
imageRectangles.Add(rect);
|
2019-12-09 02:03:45 +13:00
|
|
|
|
|
2019-12-16 01:07:40 +13:00
|
|
|
|
if (RandomAngle)
|
2019-12-09 02:03:45 +13:00
|
|
|
|
{
|
|
|
|
|
float moveX = rect.X + (rect.Width / 2f);
|
|
|
|
|
float moveY = rect.Y + (rect.Height / 2f);
|
2020-04-24 01:41:09 +12:00
|
|
|
|
int rotate = RandomFast.Next(Math.Min(RandomAngleMin, RandomAngleMax), Math.Max(RandomAngleMin, RandomAngleMax));
|
2019-12-09 22:26:42 +13:00
|
|
|
|
|
2019-12-09 02:03:45 +13:00
|
|
|
|
g.TranslateTransform(moveX, moveY);
|
2019-12-09 22:26:42 +13:00
|
|
|
|
g.RotateTransform(rotate);
|
2019-12-09 02:03:45 +13:00
|
|
|
|
g.TranslateTransform(-moveX, -moveY);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 22:26:42 +13:00
|
|
|
|
if (RandomOpacity)
|
|
|
|
|
{
|
2020-04-24 01:41:09 +12:00
|
|
|
|
float opacity = RandomFast.Next(Math.Min(RandomOpacityMin, RandomOpacityMax), Math.Max(RandomOpacityMin, RandomOpacityMax)).Clamp(0, 100) / 100f;
|
2019-12-09 22:26:42 +13:00
|
|
|
|
|
|
|
|
|
ColorMatrix matrix = new ColorMatrix();
|
|
|
|
|
matrix.Matrix33 = opacity;
|
|
|
|
|
using (ImageAttributes attributes = new ImageAttributes())
|
|
|
|
|
{
|
|
|
|
|
attributes.SetColorMatrix(matrix);
|
|
|
|
|
g.DrawImage(img2, rect, 0, 0, img2.Width, img2.Height, GraphicsUnit.Pixel, attributes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
g.DrawImage(img2, rect);
|
|
|
|
|
}
|
2019-12-09 02:03:45 +13:00
|
|
|
|
|
2019-12-16 01:07:40 +13:00
|
|
|
|
if (RandomAngle)
|
2019-12-09 02:03:45 +13:00
|
|
|
|
{
|
|
|
|
|
g.ResetTransform();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-08 20:23:20 +13:00
|
|
|
|
}
|
|
|
|
|
}
|