ShareX/ShareX.MediaLib/ImageBeautifier.cs
2023-06-08 07:49:10 +03:00

115 lines
4.5 KiB
C#

#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2023 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.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ShareX.MediaLib
{
public class ImageBeautifier
{
public ImageBeautifierOptions Options { get; private set; }
public ImageBeautifier(ImageBeautifierOptions options)
{
Options = options;
}
public Bitmap Render(Bitmap image)
{
Bitmap resultImage = (Bitmap)image.Clone();
if (Options.SmartPadding)
{
resultImage = ImageHelpers.AutoCropImage(resultImage, true, AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right, Options.Padding);
}
else if (Options.Padding > 0)
{
Color color = resultImage.GetPixel(0, 0);
Bitmap resultImageNew = ImageHelpers.AddCanvas(resultImage, Options.Padding, color);
resultImage.Dispose();
resultImage = resultImageNew;
}
if (Options.RoundedCorner > 0)
{
resultImage = ImageHelpers.RoundedCorners(resultImage, Options.RoundedCorner);
}
if (Options.Margin > 0)
{
Bitmap resultImageNew = ImageHelpers.AddCanvas(resultImage, Options.Margin);
resultImage.Dispose();
resultImage = resultImageNew;
}
if (Options.ShadowOpacity > 0 && (Options.ShadowRadius > 0 || Options.ShadowDistance > 0))
{
float shadowOpacity = Options.ShadowOpacity / 100f;
Point shadowOffset = (Point)MathHelpers.DegreeToVector2(Options.ShadowAngle, Options.ShadowDistance);
resultImage = ImageHelpers.AddShadow(resultImage, shadowOpacity, Options.ShadowRadius, 0f, Options.ShadowColor, shadowOffset, false);
}
switch (Options.BackgroundType)
{
case ImageBeautifierBackgroundType.Gradient:
if (Options.BackgroundGradient != null && Options.BackgroundGradient.IsVisible)
{
Bitmap resultImageNew = ImageHelpers.FillBackground(resultImage, Options.BackgroundGradient);
resultImage.Dispose();
resultImage = resultImageNew;
}
break;
case ImageBeautifierBackgroundType.Color:
if (!Options.BackgroundColor.IsTransparent())
{
Bitmap resultImageNew = ImageHelpers.FillBackground(resultImage, Options.BackgroundColor);
resultImage.Dispose();
resultImage = resultImageNew;
}
break;
case ImageBeautifierBackgroundType.Image:
resultImage = ImageHelpers.DrawBackgroundImage(resultImage, Options.BackgroundImageFilePath);
break;
case ImageBeautifierBackgroundType.Desktop:
string desktopWallpaperFilePath = Helpers.GetDesktopWallpaperFilePath();
resultImage = ImageHelpers.DrawBackgroundImage(resultImage, desktopWallpaperFilePath);
break;
default:
case ImageBeautifierBackgroundType.Transparent:
break;
}
return resultImage;
}
public async Task<Bitmap> RenderAsync(Bitmap image)
{
return await Task.Run(() => Render(image));
}
}
}