2023-04-25 02:33:40 +12:00
|
|
|
|
#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;
|
2023-04-25 03:38:50 +12:00
|
|
|
|
using System;
|
2023-04-25 02:33:40 +12:00
|
|
|
|
using System.Drawing;
|
2023-04-25 03:38:50 +12:00
|
|
|
|
using System.Threading.Tasks;
|
2023-04-25 02:33:40 +12:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.MediaLib
|
|
|
|
|
{
|
|
|
|
|
public partial class ImageBeautifierForm : Form
|
|
|
|
|
{
|
|
|
|
|
public Bitmap SourceImage { get; private set; }
|
|
|
|
|
public Bitmap PreviewImage { get; private set; }
|
|
|
|
|
public ImageBeautifierOptions Options { get; private set; }
|
2023-04-25 08:14:38 +12:00
|
|
|
|
public string FilePath { get; private set; }
|
2023-04-25 02:33:40 +12:00
|
|
|
|
|
2023-04-25 08:50:36 +12:00
|
|
|
|
public event Action<Bitmap> UploadImageRequested;
|
|
|
|
|
public event Action<Bitmap> PrintImageRequested;
|
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
private bool isReady, isBusy, isPending;
|
2023-04-25 02:33:40 +12:00
|
|
|
|
|
2023-04-25 08:14:38 +12:00
|
|
|
|
private ImageBeautifierForm(ImageBeautifierOptions options = null)
|
2023-04-25 02:33:40 +12:00
|
|
|
|
{
|
|
|
|
|
Options = options;
|
|
|
|
|
|
|
|
|
|
if (Options == null)
|
|
|
|
|
{
|
|
|
|
|
Options = new ImageBeautifierOptions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
ShareXResources.ApplyTheme(this);
|
|
|
|
|
|
|
|
|
|
tbMargin.SetValue(Options.Margin);
|
|
|
|
|
tbPadding.SetValue(Options.Padding);
|
|
|
|
|
cbSmartPadding.Checked = Options.SmartPadding;
|
|
|
|
|
tbRoundedCorner.SetValue(Options.RoundedCorner);
|
|
|
|
|
tbShadowSize.SetValue(Options.ShadowSize);
|
2023-04-25 03:38:50 +12:00
|
|
|
|
UpdateUI();
|
2023-04-25 06:27:23 +12:00
|
|
|
|
UpdateBackgroundPreview();
|
2023-04-25 02:33:40 +12:00
|
|
|
|
|
|
|
|
|
isReady = true;
|
2023-04-25 03:38:50 +12:00
|
|
|
|
}
|
2023-04-25 02:33:40 +12:00
|
|
|
|
|
2023-04-25 08:14:38 +12:00
|
|
|
|
public ImageBeautifierForm(Bitmap sourceImage, ImageBeautifierOptions options = null) : this(options)
|
|
|
|
|
{
|
|
|
|
|
SourceImage = sourceImage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ImageBeautifierForm(string filePath, ImageBeautifierOptions options = null) : this(options)
|
|
|
|
|
{
|
|
|
|
|
FilePath = filePath;
|
|
|
|
|
SourceImage = ImageHelpers.LoadImage(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
private void UpdateUI()
|
|
|
|
|
{
|
|
|
|
|
lblMarginValue.Text = tbMargin.Value.ToString();
|
|
|
|
|
lblPaddingValue.Text = tbPadding.Value.ToString();
|
|
|
|
|
lblRoundedCornerValue.Text = tbRoundedCorner.Value.ToString();
|
|
|
|
|
lblShadowSizeValue.Text = tbShadowSize.Value.ToString();
|
2023-04-25 02:33:40 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 06:27:23 +12:00
|
|
|
|
private void UpdateBackgroundPreview()
|
|
|
|
|
{
|
|
|
|
|
pbBackground.Image?.Dispose();
|
|
|
|
|
pbBackground.Image = Options.Background.CreateGradientPreview(pbBackground.ClientRectangle.Width, pbBackground.ClientRectangle.Height, true, true);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
private async Task UpdatePreview()
|
2023-04-25 02:33:40 +12:00
|
|
|
|
{
|
|
|
|
|
if (isReady)
|
|
|
|
|
{
|
2023-04-25 03:38:50 +12:00
|
|
|
|
UpdateUI();
|
2023-04-25 02:33:40 +12:00
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
if (isBusy)
|
|
|
|
|
{
|
|
|
|
|
isPending = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
isBusy = true;
|
2023-04-25 02:33:40 +12:00
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
UpdateOptions();
|
2023-04-25 02:33:40 +12:00
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
Bitmap resultImage = await RenderPreview(SourceImage, Options);
|
|
|
|
|
PreviewImage?.Dispose();
|
|
|
|
|
PreviewImage = resultImage;
|
2023-04-25 06:27:23 +12:00
|
|
|
|
pbPreview.LoadImage(PreviewImage);
|
2023-04-25 02:33:40 +12:00
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
isBusy = false;
|
2023-04-25 02:33:40 +12:00
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
if (isPending)
|
|
|
|
|
{
|
|
|
|
|
isPending = false;
|
2023-04-25 02:33:40 +12:00
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
await UpdatePreview();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-25 02:33:40 +12:00
|
|
|
|
}
|
2023-04-25 03:38:50 +12:00
|
|
|
|
}
|
2023-04-25 02:33:40 +12:00
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
private static async Task<Bitmap> RenderPreview(Bitmap sourceImage, ImageBeautifierOptions options)
|
|
|
|
|
{
|
|
|
|
|
return await Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
Bitmap resultImage = (Bitmap)sourceImage.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.ShadowSize > 0)
|
|
|
|
|
{
|
|
|
|
|
resultImage = ImageHelpers.AddShadow(resultImage, 1f, options.ShadowSize, 0f, Color.Black, new Point(0, 0), false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.Background != null)
|
|
|
|
|
{
|
|
|
|
|
Bitmap resultImageNew = ImageHelpers.FillBackground(resultImage, options.Background);
|
|
|
|
|
resultImage.Dispose();
|
|
|
|
|
resultImage = resultImageNew;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return resultImage;
|
|
|
|
|
});
|
2023-04-25 02:33:40 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateOptions()
|
|
|
|
|
{
|
|
|
|
|
Options.Margin = tbMargin.Value;
|
|
|
|
|
Options.Padding = tbPadding.Value;
|
|
|
|
|
Options.SmartPadding = cbSmartPadding.Checked;
|
|
|
|
|
Options.RoundedCorner = tbRoundedCorner.Value;
|
|
|
|
|
Options.ShadowSize = tbShadowSize.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 08:50:36 +12:00
|
|
|
|
private void OnUploadImageRequested()
|
|
|
|
|
{
|
|
|
|
|
UploadImageRequested?.Invoke(PreviewImage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPrintImageRequested()
|
|
|
|
|
{
|
|
|
|
|
PrintImageRequested?.Invoke(PreviewImage);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
private async void ImageBeautifierForm_Shown(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
await UpdatePreview();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void tbMargin_Scroll(object sender, EventArgs e)
|
2023-04-25 02:33:40 +12:00
|
|
|
|
{
|
2023-04-25 03:38:50 +12:00
|
|
|
|
await UpdatePreview();
|
2023-04-25 02:33:40 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
private async void tbPadding_Scroll(object sender, EventArgs e)
|
2023-04-25 02:33:40 +12:00
|
|
|
|
{
|
2023-04-25 03:38:50 +12:00
|
|
|
|
await UpdatePreview();
|
2023-04-25 02:33:40 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
private async void cbSmartPadding_CheckedChanged(object sender, EventArgs e)
|
2023-04-25 02:33:40 +12:00
|
|
|
|
{
|
2023-04-25 03:38:50 +12:00
|
|
|
|
await UpdatePreview();
|
2023-04-25 02:33:40 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
private async void tbRoundedCorner_Scroll(object sender, EventArgs e)
|
2023-04-25 02:33:40 +12:00
|
|
|
|
{
|
2023-04-25 03:38:50 +12:00
|
|
|
|
await UpdatePreview();
|
2023-04-25 02:33:40 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 08:14:38 +12:00
|
|
|
|
private void btnCopy_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (PreviewImage != null)
|
|
|
|
|
{
|
|
|
|
|
ClipboardHelpers.CopyImage(PreviewImage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnSave_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (PreviewImage != null && !string.IsNullOrEmpty(FilePath))
|
|
|
|
|
{
|
|
|
|
|
ImageHelpers.SaveImage(PreviewImage, FilePath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnSaveAs_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (PreviewImage != null)
|
|
|
|
|
{
|
|
|
|
|
string filePath = ImageHelpers.SaveImageFileDialog(PreviewImage, FilePath);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(filePath))
|
|
|
|
|
{
|
|
|
|
|
FilePath = filePath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnUpload_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-04-25 08:50:36 +12:00
|
|
|
|
if (PreviewImage != null)
|
|
|
|
|
{
|
|
|
|
|
OnUploadImageRequested();
|
|
|
|
|
}
|
2023-04-25 08:14:38 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnPrint_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2023-04-25 08:50:36 +12:00
|
|
|
|
if (PreviewImage != null)
|
|
|
|
|
{
|
|
|
|
|
OnPrintImageRequested();
|
|
|
|
|
}
|
2023-04-25 08:14:38 +12:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 03:38:50 +12:00
|
|
|
|
private async void tbShadowSize_Scroll(object sender, EventArgs e)
|
2023-04-25 02:33:40 +12:00
|
|
|
|
{
|
2023-04-25 03:38:50 +12:00
|
|
|
|
await UpdatePreview();
|
2023-04-25 02:33:40 +12:00
|
|
|
|
}
|
2023-04-25 06:27:23 +12:00
|
|
|
|
|
|
|
|
|
private async void pbBackground_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
using (GradientPickerForm gradientPickerForm = new GradientPickerForm(Options.Background.Copy()))
|
|
|
|
|
{
|
|
|
|
|
if (gradientPickerForm.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
Options.Background = gradientPickerForm.Gradient;
|
|
|
|
|
UpdateBackgroundPreview();
|
|
|
|
|
|
|
|
|
|
await UpdatePreview();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-25 02:33:40 +12:00
|
|
|
|
}
|
|
|
|
|
}
|