From d0ce706da5565b67af2b0a16176499512bf3d33a Mon Sep 17 00:00:00 2001 From: Jaex Date: Thu, 8 Jun 2023 07:49:10 +0300 Subject: [PATCH] Use ImageBeautifier class --- ShareX.MediaLib/Forms/ImageBeautifierForm.cs | 4 +- ShareX.MediaLib/ImageBeautifier.cs | 115 +++++++++++++++++++ ShareX.MediaLib/ImageBeautifierOptions.cs | 75 ------------ ShareX.MediaLib/ShareX.MediaLib.csproj | 1 + 4 files changed, 119 insertions(+), 76 deletions(-) create mode 100644 ShareX.MediaLib/ImageBeautifier.cs diff --git a/ShareX.MediaLib/Forms/ImageBeautifierForm.cs b/ShareX.MediaLib/Forms/ImageBeautifierForm.cs index 006dd692f..4fb7cc320 100644 --- a/ShareX.MediaLib/Forms/ImageBeautifierForm.cs +++ b/ShareX.MediaLib/Forms/ImageBeautifierForm.cs @@ -45,6 +45,7 @@ public partial class ImageBeautifierForm : Form private bool isReady, isBusy, isPending, pendingQuickRender; private string title; + private ImageBeautifier imageBeautifier; private ImageBeautifierForm(ImageBeautifierOptions options = null) { @@ -58,6 +59,7 @@ private ImageBeautifierForm(ImageBeautifierOptions options = null) InitializeComponent(); ShareXResources.ApplyTheme(this, true); title = Text; + imageBeautifier = new ImageBeautifier(Options); LoadOptions(); } @@ -169,7 +171,7 @@ private async Task UpdatePreview(bool quickRender = false) } Stopwatch renderTime = Stopwatch.StartNew(); - Bitmap resultImage = await options.RenderAsync(SourceImage); + Bitmap resultImage = await imageBeautifier.RenderAsync(SourceImage); renderTime.Stop(); if (IsDisposed) diff --git a/ShareX.MediaLib/ImageBeautifier.cs b/ShareX.MediaLib/ImageBeautifier.cs new file mode 100644 index 000000000..128035753 --- /dev/null +++ b/ShareX.MediaLib/ImageBeautifier.cs @@ -0,0 +1,115 @@ +#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 . +*/ + +#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 RenderAsync(Bitmap image) + { + return await Task.Run(() => Render(image)); + } + } +} \ No newline at end of file diff --git a/ShareX.MediaLib/ImageBeautifierOptions.cs b/ShareX.MediaLib/ImageBeautifierOptions.cs index eb07a22e3..24a2df7f1 100644 --- a/ShareX.MediaLib/ImageBeautifierOptions.cs +++ b/ShareX.MediaLib/ImageBeautifierOptions.cs @@ -26,8 +26,6 @@ You should have received a copy of the GNU General Public License using ShareX.HelpersLib; using System.Drawing; using System.Drawing.Drawing2D; -using System.Threading.Tasks; -using System.Windows.Forms; namespace ShareX.MediaLib { @@ -68,78 +66,5 @@ public void ResetOptions() BackgroundColor = Color.FromArgb(34, 34, 34); BackgroundImageFilePath = ""; } - - public Bitmap Render(Bitmap image) - { - Bitmap resultImage = (Bitmap)image.Clone(); - - if (SmartPadding) - { - resultImage = ImageHelpers.AutoCropImage(resultImage, true, AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right, Padding); - } - else if (Padding > 0) - { - Color color = resultImage.GetPixel(0, 0); - Bitmap resultImageNew = ImageHelpers.AddCanvas(resultImage, Padding, color); - resultImage.Dispose(); - resultImage = resultImageNew; - } - - if (RoundedCorner > 0) - { - resultImage = ImageHelpers.RoundedCorners(resultImage, RoundedCorner); - } - - if (Margin > 0) - { - Bitmap resultImageNew = ImageHelpers.AddCanvas(resultImage, Margin); - resultImage.Dispose(); - resultImage = resultImageNew; - } - - if (ShadowOpacity > 0 && (ShadowRadius > 0 || ShadowDistance > 0)) - { - float shadowOpacity = ShadowOpacity / 100f; - Point shadowOffset = (Point)MathHelpers.DegreeToVector2(ShadowAngle, ShadowDistance); - resultImage = ImageHelpers.AddShadow(resultImage, shadowOpacity, ShadowRadius, 0f, ShadowColor, shadowOffset, false); - } - - switch (BackgroundType) - { - case ImageBeautifierBackgroundType.Gradient: - if (BackgroundGradient != null && BackgroundGradient.IsVisible) - { - Bitmap resultImageNew = ImageHelpers.FillBackground(resultImage, BackgroundGradient); - resultImage.Dispose(); - resultImage = resultImageNew; - } - break; - case ImageBeautifierBackgroundType.Color: - if (!BackgroundColor.IsTransparent()) - { - Bitmap resultImageNew = ImageHelpers.FillBackground(resultImage, BackgroundColor); - resultImage.Dispose(); - resultImage = resultImageNew; - } - break; - case ImageBeautifierBackgroundType.Image: - resultImage = ImageHelpers.DrawBackgroundImage(resultImage, 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 RenderAsync(Bitmap image) - { - return await Task.Run(() => Render(image)); - } } } \ No newline at end of file diff --git a/ShareX.MediaLib/ShareX.MediaLib.csproj b/ShareX.MediaLib/ShareX.MediaLib.csproj index 9843e621c..612ee0511 100644 --- a/ShareX.MediaLib/ShareX.MediaLib.csproj +++ b/ShareX.MediaLib/ShareX.MediaLib.csproj @@ -123,6 +123,7 @@ VideoThumbnailerForm.cs +