mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-29 17:02:05 +13:00
Use ImageBeautifier class
This commit is contained in:
parent
ce4932cdb6
commit
d0ce706da5
4 changed files with 119 additions and 76 deletions
|
@ -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)
|
||||
|
|
115
ShareX.MediaLib/ImageBeautifier.cs
Normal file
115
ShareX.MediaLib/ImageBeautifier.cs
Normal file
|
@ -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 <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));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<Bitmap> RenderAsync(Bitmap image)
|
||||
{
|
||||
return await Task.Run(() => Render(image));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -123,6 +123,7 @@
|
|||
<Compile Include="Forms\VideoThumbnailerForm.Designer.cs">
|
||||
<DependentUpon>VideoThumbnailerForm.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ImageBeautifier.cs" />
|
||||
<Compile Include="ImageBeautifierOptions.cs" />
|
||||
<Compile Include="ImageCombinerOptions.cs" />
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
|
|
Loading…
Reference in a new issue