ShareX/ShareX.ImageEffectsLib/Drawings/DrawParticles.cs

221 lines
6.8 KiB
C#
Raw Normal View History

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
{
[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
{
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>();
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
{
string imageFolder = Helpers.ExpandFolderVariables(ImageFolder);
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))
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++)
{
2019-12-09 02:39:23 +13:00
string file = MathHelpers.RandomPick(files);
2019-12-08 20:23:20 +13:00
Bitmap bmpCached = imageCache.GetImage(file);
if (bmpCached != null)
2019-12-08 20:23:20 +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)
{
2019-12-18 07:19:44 +13:00
int size = MathHelpers.Random(Math.Min(RandomSizeMin, RandomSizeMax), Math.Max(RandomSizeMin, RandomSizeMax));
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
rect = new Rectangle(MathHelpers.Random(Math.Min(0, xOffset), Math.Max(0, xOffset)),
MathHelpers.Random(Math.Min(0, yOffset), Math.Max(0, yOffset)), width, height);
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);
2019-12-16 01:07:40 +13:00
int rotate = MathHelpers.Random(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)
{
float opacity = MathHelpers.Random(Math.Min(RandomOpacityMin, RandomOpacityMax), Math.Max(RandomOpacityMin, RandomOpacityMax)).Clamp(0, 100) / 100f;
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
}
}