2013-11-03 23:53:49 +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
2013-11-03 23:53:49 +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 )
2014-12-11 09:25:20 +13:00
using ShareX.HelpersLib ;
using ShareX.ImageEffectsLib.Properties ;
2013-11-03 23:53:49 +13:00
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
using System.Drawing ;
using System.Windows.Forms ;
2014-12-11 09:25:20 +13:00
namespace ShareX.ImageEffectsLib
2013-11-03 23:53:49 +13:00
{
2016-02-26 09:55:54 +13:00
public partial class ImageEffectsForm : Form
2013-11-03 23:53:49 +13:00
{
2020-07-06 11:23:59 +12:00
public static bool IsInstanceActive = > instance ! = null & & ! instance . IsDisposed ;
private static ImageEffectsForm instance ;
2020-03-24 08:33:26 +13:00
public event Action < Bitmap > ImageProcessRequested ;
2018-05-09 10:34:19 +12:00
2019-09-23 20:16:52 +12:00
public bool AutoGeneratePreviewImage { get ; set ; }
2020-03-21 08:02:26 +13:00
public Bitmap PreviewImage { get ; private set ; }
2017-09-24 00:07:18 +12:00
public List < ImageEffectPreset > Presets { get ; private set ; }
2017-09-24 00:48:28 +12:00
public int SelectedPresetIndex { get ; private set ; }
2019-11-05 23:56:08 +13:00
public string FilePath { get ; private set ; }
2013-11-03 23:53:49 +13:00
2020-07-09 01:12:43 +12:00
private bool pauseUpdate ;
2017-09-24 00:07:18 +12:00
2020-03-21 08:02:26 +13:00
public ImageEffectsForm ( Bitmap bmp , List < ImageEffectPreset > presets , int selectedPresetIndex )
2013-11-03 23:53:49 +13:00
{
2020-07-06 11:23:59 +12:00
pauseUpdate = true ;
2013-11-03 23:53:49 +13:00
InitializeComponent ( ) ;
2019-06-25 06:36:16 +12:00
ShareXResources . ApplyTheme ( this ) ;
2019-06-24 01:59:48 +12:00
2020-03-21 08:02:26 +13:00
PreviewImage = bmp ;
2019-09-23 20:16:52 +12:00
if ( PreviewImage = = null )
{
AutoGeneratePreviewImage = true ;
}
2017-09-24 00:07:18 +12:00
Presets = presets ;
2018-01-21 23:12:32 +13:00
if ( Presets . Count = = 0 )
{
Presets . Add ( new ImageEffectPreset ( ) ) ;
}
2019-09-23 20:16:52 +12:00
2017-09-24 00:48:28 +12:00
SelectedPresetIndex = selectedPresetIndex ;
2020-07-06 11:23:59 +12:00
2017-09-24 00:48:28 +12:00
eiImageEffects . ObjectType = typeof ( ImageEffectPreset ) ;
2020-05-15 10:57:55 +12:00
eiImageEffects . SerializationBinder = new TypeNameSerializationBinder ( "ShareX.ImageEffectsLib" , "ShareX.ImageEffectsLib" ) ;
2020-07-06 11:23:59 +12:00
2014-11-11 10:40:34 +13:00
AddAllEffectsToContextMenu ( ) ;
2020-07-06 11:23:59 +12:00
LoadSettings ( ) ;
}
public static ImageEffectsForm GetFormInstance ( List < ImageEffectPreset > presets , int selectedPresetIndex )
{
if ( ! IsInstanceActive )
{
instance = new ImageEffectsForm ( null , presets , selectedPresetIndex ) ;
}
return instance ;
2013-11-06 13:59:13 +13:00
}
2013-11-03 23:53:49 +13:00
2020-03-24 08:33:26 +13:00
public void EnableToolMode ( Action < Bitmap > imageProcessRequested , string filePath = null )
2013-11-06 13:59:13 +13:00
{
2019-11-05 23:56:08 +13:00
FilePath = filePath ;
2018-05-09 10:34:19 +12:00
ImageProcessRequested + = imageProcessRequested ;
2013-11-12 19:35:44 +13:00
pbResult . AllowDrop = true ;
2015-03-31 19:09:34 +13:00
mbLoadImage . Visible = true ;
2013-11-08 14:14:04 +13:00
btnSaveImage . Visible = true ;
2018-05-09 10:34:19 +12:00
btnUploadImage . Visible = true ;
2013-11-03 23:53:49 +13:00
}
2018-01-21 11:40:38 +13:00
public void EditorMode ( )
{
btnOK . Visible = true ;
2018-02-19 15:35:57 +13:00
btnClose . Text = Resources . ImageEffectsForm_EditorMode_Cancel ;
2018-01-21 11:40:38 +13:00
}
2020-07-06 11:23:59 +12:00
public void ImportImageEffect ( string json )
{
eiImageEffects . ImportJson ( json ) ;
}
2020-03-24 08:33:26 +13:00
protected void OnImageProcessRequested ( Bitmap bmp )
2018-05-09 10:34:19 +12:00
{
2020-06-07 12:52:02 +12:00
ImageProcessRequested ? . Invoke ( bmp ) ;
2018-05-09 10:34:19 +12:00
}
2014-11-11 10:40:34 +13:00
private void AddAllEffectsToContextMenu ( )
2013-11-03 23:53:49 +13:00
{
2014-11-11 10:40:34 +13:00
AddEffectToContextMenu ( Resources . ImageEffectsForm_AddAllEffectsToTreeView_Drawings ,
2013-11-09 16:13:49 +13:00
typeof ( DrawBackground ) ,
typeof ( DrawBorder ) ,
2013-11-17 02:24:03 +13:00
typeof ( DrawCheckerboard ) ,
2013-11-11 13:48:31 +13:00
typeof ( DrawImage ) ,
2020-07-20 09:33:16 +12:00
typeof ( DrawTextEx ) ,
2020-07-21 05:19:51 +12:00
typeof ( DrawText ) ,
2019-12-18 07:27:08 +13:00
typeof ( DrawParticles ) ) ;
2013-11-09 16:13:49 +13:00
2014-11-11 10:40:34 +13:00
AddEffectToContextMenu ( Resources . ImageEffectsForm_AddAllEffectsToTreeView_Manipulations ,
2018-01-22 00:32:15 +13:00
typeof ( AutoCrop ) ,
2013-11-06 06:58:22 +13:00
typeof ( Canvas ) ,
2013-11-13 14:29:20 +13:00
typeof ( Crop ) ,
2013-11-06 06:58:22 +13:00
typeof ( Flip ) ,
2020-07-16 06:16:43 +12:00
typeof ( ForceProportions ) ,
2013-11-06 06:58:22 +13:00
typeof ( Resize ) ,
typeof ( Rotate ) ,
2014-10-20 10:48:54 +13:00
typeof ( RoundedCorners ) ,
2013-11-06 06:58:22 +13:00
typeof ( Scale ) ,
typeof ( Skew ) ) ;
2014-11-11 10:40:34 +13:00
AddEffectToContextMenu ( Resources . ImageEffectsForm_AddAllEffectsToTreeView_Adjustments ,
2013-11-06 06:58:22 +13:00
typeof ( Alpha ) ,
typeof ( BlackWhite ) ,
typeof ( Brightness ) ,
typeof ( Colorize ) ,
typeof ( Contrast ) ,
typeof ( Gamma ) ,
typeof ( Grayscale ) ,
typeof ( Hue ) ,
typeof ( Inverse ) ,
2013-11-06 12:10:27 +13:00
typeof ( MatrixColor ) ,
2013-11-06 06:58:22 +13:00
typeof ( Polaroid ) ,
typeof ( Saturation ) ,
2018-05-06 13:56:38 +12:00
typeof ( SelectiveColor ) ,
2013-11-06 06:58:22 +13:00
typeof ( Sepia ) ) ;
2014-11-11 10:40:34 +13:00
AddEffectToContextMenu ( Resources . ImageEffectsForm_AddAllEffectsToTreeView_Filters ,
2013-11-06 06:58:22 +13:00
typeof ( Blur ) ,
2013-11-06 11:48:41 +13:00
typeof ( EdgeDetect ) ,
typeof ( Emboss ) ,
typeof ( GaussianBlur ) ,
2013-11-06 12:10:27 +13:00
typeof ( MatrixConvolution ) ,
2013-11-06 11:48:41 +13:00
typeof ( MeanRemoval ) ,
2014-10-21 08:37:51 +13:00
typeof ( Outline ) ,
2013-11-06 06:58:22 +13:00
typeof ( Pixelate ) ,
typeof ( Reflection ) ,
2020-07-06 10:37:56 +12:00
typeof ( RGBSplit ) ,
2013-11-06 06:58:22 +13:00
typeof ( Shadow ) ,
typeof ( Sharpen ) ,
2019-06-26 03:33:29 +12:00
typeof ( Slice ) ,
2013-11-06 11:48:41 +13:00
typeof ( Smooth ) ,
2013-11-06 06:58:22 +13:00
typeof ( TornEdge ) ) ;
2013-11-03 23:53:49 +13:00
}
2014-11-11 10:40:34 +13:00
private void AddEffectToContextMenu ( string groupName , params Type [ ] imageEffects )
2013-11-03 23:53:49 +13:00
{
2014-11-11 10:40:34 +13:00
ToolStripMenuItem tsmiParent = new ToolStripMenuItem ( groupName ) ;
2015-09-24 06:23:25 +12:00
tsmiParent . HideImageMargin ( ) ;
2014-11-15 09:43:40 +13:00
2014-11-11 10:40:34 +13:00
cmsEffects . Items . Add ( tsmiParent ) ;
2013-11-03 23:53:49 +13:00
foreach ( Type imageEffect in imageEffects )
{
2014-11-15 09:43:40 +13:00
ToolStripItem tsmiChild = tsmiParent . DropDownItems . Add ( imageEffect . GetDescription ( ) . Replace ( "&" , "&&" ) ) ;
2014-11-11 10:40:34 +13:00
tsmiChild . Tag = imageEffect ;
tsmiChild . Click + = tsmiEffectClick ;
2013-11-03 23:53:49 +13:00
}
}
2017-09-25 20:49:08 +13:00
private void LoadSettings ( )
{
2020-07-08 18:31:15 +12:00
if ( Presets ! = null & & Presets . Count > 0 )
2017-09-25 20:49:08 +13:00
{
2020-07-08 18:31:15 +12:00
foreach ( ImageEffectPreset preset in Presets )
{
2020-07-08 23:14:24 +12:00
ListViewItem lvi = new ListViewItem ( preset . ToString ( ) ) ;
lvPresets . Items . Add ( lvi ) ;
2020-07-08 18:31:15 +12:00
}
2017-09-25 20:49:08 +13:00
2020-07-08 23:14:24 +12:00
lvPresets . SelectedIndex = SelectedPresetIndex . Clamp ( 0 , Presets . Count - 1 ) ;
2017-09-25 20:49:08 +13:00
}
2017-10-15 01:26:36 +13:00
UpdateControlStates ( ) ;
2017-09-25 20:49:08 +13:00
}
2017-09-24 00:07:18 +12:00
private ImageEffectPreset GetSelectedPreset ( )
{
2020-07-08 23:14:24 +12:00
return GetSelectedPreset ( out _ ) ;
}
private ImageEffectPreset GetSelectedPreset ( out ListViewItem lvi )
{
int index = lvPresets . SelectedIndex ;
2017-09-24 00:07:18 +12:00
if ( Presets . IsValidIndex ( index ) )
{
2020-07-08 23:14:24 +12:00
lvi = lvPresets . Items [ index ] ;
2017-09-24 00:07:18 +12:00
return Presets [ index ] ;
}
2020-07-08 23:14:24 +12:00
lvi = null ;
2017-09-24 00:07:18 +12:00
return null ;
}
private void AddPreset ( )
{
AddPreset ( new ImageEffectPreset ( ) ) ;
}
private void AddPreset ( ImageEffectPreset preset )
{
if ( preset ! = null )
{
Presets . Add ( preset ) ;
2020-07-08 23:14:24 +12:00
ListViewItem lvi = new ListViewItem ( preset . ToString ( ) ) ;
lvPresets . Items . Add ( lvi ) ;
lvPresets . SelectLast ( ) ;
2017-09-24 00:48:28 +12:00
txtPresetName . Focus ( ) ;
2017-09-24 00:07:18 +12:00
}
}
2013-11-03 23:53:49 +13:00
private void UpdatePreview ( )
{
2017-09-24 01:17:21 +12:00
if ( ! pauseUpdate )
2013-11-03 23:53:49 +13:00
{
2017-09-24 01:17:21 +12:00
ImageEffectPreset preset = GetSelectedPreset ( ) ;
2013-11-21 06:59:00 +13:00
2019-09-23 20:16:52 +12:00
if ( preset ! = null )
2013-11-21 06:59:00 +13:00
{
2018-05-09 11:10:38 +12:00
Cursor = Cursors . WaitCursor ;
2017-09-24 01:17:21 +12:00
2018-05-09 11:10:38 +12:00
try
2016-03-18 22:00:51 +13:00
{
2019-09-23 20:16:52 +12:00
if ( AutoGeneratePreviewImage )
{
2020-07-09 21:26:09 +12:00
GeneratePreviewImage ( 24 ) ;
2019-09-23 20:16:52 +12:00
}
2018-05-09 11:10:38 +12:00
2019-09-23 20:16:52 +12:00
if ( PreviewImage ! = null )
2017-09-24 01:17:21 +12:00
{
2020-07-09 01:12:43 +12:00
//Debug.WriteLine("Updating preview...");
2019-09-23 20:16:52 +12:00
Stopwatch timer = Stopwatch . StartNew ( ) ;
using ( Image preview = ApplyEffects ( ) )
2018-05-09 11:10:38 +12:00
{
2019-09-23 20:16:52 +12:00
if ( preview ! = null )
{
pbResult . LoadImage ( preview ) ;
Text = string . Format ( "ShareX - " + Resources . ImageEffectsForm_UpdatePreview_Image_effects___Width___0___Height___1___Render_time___2__ms ,
preview . Width , preview . Height , timer . ElapsedMilliseconds ) ;
}
else
{
pbResult . Reset ( ) ;
Text = string . Format ( "ShareX - " + Resources . ImageEffectsForm_UpdatePreview_Image_effects___Width___0___Height___1___Render_time___2__ms ,
0 , 0 , timer . ElapsedMilliseconds ) ;
}
2018-05-09 11:10:38 +12:00
}
2017-09-24 01:17:21 +12:00
}
2016-03-18 22:00:51 +13:00
}
2018-05-09 11:10:38 +12:00
finally
{
Cursor = Cursors . Default ;
}
2013-11-21 06:59:00 +13:00
}
2020-07-08 23:54:46 +12:00
else
{
pbResult . Reset ( ) ;
}
2017-09-27 23:42:20 +13:00
UpdateControlStates ( ) ;
2013-11-03 23:53:49 +13:00
}
}
2017-09-27 23:42:20 +13:00
private void UpdateControlStates ( )
{
2020-07-08 23:47:21 +12:00
btnPresetRemove . Enabled = btnPresetDuplicate . Enabled = txtPresetName . Enabled = btnEffectAdd . Enabled = lvPresets . SelectedItems . Count > 0 ;
btnEffectClear . Enabled = btnEffectRefresh . Enabled = eiImageEffects . Enabled = btnPackager . Enabled = lvEffects . Items . Count > 0 ;
2020-07-08 23:14:24 +12:00
btnEffectRemove . Enabled = btnEffectDuplicate . Enabled = lvEffects . SelectedItems . Count > 0 ;
2017-09-27 23:42:20 +13:00
}
2019-09-23 20:16:52 +12:00
private void GeneratePreviewImage ( int padding )
{
if ( pbResult . ClientSize . Width > 0 & & pbResult . ClientSize . Height > 0 )
{
2020-07-09 21:26:09 +12:00
int size = Math . Min ( pbResult . ClientSize . Width , pbResult . ClientSize . Height ) ;
2019-09-23 20:16:52 +12:00
int minSizePadding = 300 ;
2020-07-09 21:26:09 +12:00
if ( size < minSizePadding + ( padding * 2 ) )
2019-09-23 20:16:52 +12:00
{
2020-07-09 21:26:09 +12:00
padding = 0 ;
2019-09-23 20:16:52 +12:00
}
2020-07-09 21:26:09 +12:00
size = size - ( padding * 2 ) ;
2019-09-23 20:16:52 +12:00
2019-09-23 22:05:03 +12:00
if ( PreviewImage ! = null ) PreviewImage . Dispose ( ) ;
2020-07-09 21:26:09 +12:00
PreviewImage = new Bitmap ( size , size ) ;
2019-09-23 22:05:03 +12:00
Color backgroundColor ;
2020-02-05 21:13:55 +13:00
if ( ShareXResources . UseCustomTheme )
2019-09-23 22:05:03 +12:00
{
backgroundColor = ShareXResources . Theme . BackgroundColor ;
}
else
2019-09-23 20:16:52 +12:00
{
2019-09-23 22:05:03 +12:00
backgroundColor = Color . DarkGray ;
}
2019-09-23 20:16:52 +12:00
2019-09-23 22:05:03 +12:00
using ( Graphics g = Graphics . FromImage ( PreviewImage ) )
{
g . Clear ( backgroundColor ) ;
2019-09-23 20:16:52 +12:00
2019-09-23 22:05:03 +12:00
if ( PreviewImage . Width > 260 & & PreviewImage . Height > 260 )
2019-09-23 20:16:52 +12:00
{
2020-03-22 11:07:38 +13:00
using ( Bitmap logo = ShareXResources . Logo )
2019-09-23 22:05:03 +12:00
{
g . DrawImage ( logo , ( PreviewImage . Width / 2 ) - ( logo . Width / 2 ) , ( PreviewImage . Height / 2 ) - ( logo . Height / 2 ) ) ;
}
2019-09-23 20:16:52 +12:00
}
}
}
}
2020-03-24 08:33:26 +13:00
private Bitmap ApplyEffects ( )
2013-11-03 23:53:49 +13:00
{
2017-09-24 00:07:18 +12:00
ImageEffectPreset preset = GetSelectedPreset ( ) ;
if ( preset ! = null )
{
2019-09-23 20:16:52 +12:00
return preset . ApplyEffects ( PreviewImage ) ;
2017-09-24 00:07:18 +12:00
}
return null ;
2013-11-03 23:53:49 +13:00
}
2014-11-11 10:40:34 +13:00
private void tsmiEffectClick ( object sender , EventArgs e )
2013-11-03 23:53:49 +13:00
{
2017-09-24 00:07:18 +12:00
ImageEffectPreset preset = GetSelectedPreset ( ) ;
2013-11-03 23:53:49 +13:00
2017-09-24 00:07:18 +12:00
if ( preset ! = null )
2013-11-03 23:53:49 +13:00
{
2017-09-24 00:07:18 +12:00
ToolStripMenuItem tsmi = sender as ToolStripMenuItem ;
if ( tsmi ! = null & & tsmi . Tag is Type )
{
Type type = ( Type ) tsmi . Tag ;
ImageEffect imageEffect = ( ImageEffect ) Activator . CreateInstance ( type ) ;
2017-09-25 19:58:16 +13:00
AddEffect ( imageEffect , preset ) ;
2017-09-24 00:07:18 +12:00
UpdatePreview ( ) ;
}
2013-11-03 23:53:49 +13:00
}
}
private void RemoveSelectedEffects ( )
{
2017-09-24 01:17:21 +12:00
ImageEffectPreset preset = GetSelectedPreset ( ) ;
if ( preset ! = null )
2013-11-03 23:53:49 +13:00
{
2017-09-24 01:17:21 +12:00
int index = lvEffects . SelectedIndex ;
if ( index > - 1 )
2013-11-03 23:53:49 +13:00
{
2017-09-24 01:17:21 +12:00
preset . Effects . RemoveAt ( index ) ;
lvEffects . Items . RemoveAt ( index ) ;
2013-11-03 23:53:49 +13:00
2020-07-09 00:07:46 +12:00
if ( lvEffects . Items . Count > 0 )
{
lvEffects . SelectedIndex = index = = lvEffects . Items . Count ? lvEffects . Items . Count - 1 : index ;
}
2017-09-24 01:17:21 +12:00
UpdatePreview ( ) ;
}
2013-11-03 23:53:49 +13:00
}
}
2017-09-24 00:07:18 +12:00
private void ClearFields ( )
2013-11-03 23:53:49 +13:00
{
2017-09-24 00:07:18 +12:00
txtPresetName . Text = "" ;
lvEffects . Items . Clear ( ) ;
2020-07-09 23:12:17 +12:00
ClearSelectedEffect ( ) ;
2013-11-03 23:53:49 +13:00
UpdatePreview ( ) ;
}
2020-07-09 23:12:17 +12:00
private void ClearSelectedEffect ( )
{
// TODO: Translate
lblEffect . Text = "Effect:" ;
pgSettings . SelectedObject = null ;
}
2017-09-25 19:58:16 +13:00
private void AddEffect ( ImageEffect imageEffect , ImageEffectPreset preset = null )
2013-11-03 23:53:49 +13:00
{
2013-11-09 16:13:49 +13:00
ListViewItem lvi = new ListViewItem ( imageEffect . GetType ( ) . GetDescription ( ) ) ;
2014-10-20 03:02:09 +13:00
lvi . Checked = imageEffect . Enabled ;
2013-11-03 23:53:49 +13:00
lvi . Tag = imageEffect ;
if ( lvEffects . SelectedIndices . Count > 0 )
{
2017-09-25 19:58:16 +13:00
int index = lvEffects . SelectedIndices [ lvEffects . SelectedIndices . Count - 1 ] + 1 ;
lvEffects . Items . Insert ( index , lvi ) ;
if ( preset ! = null )
{
preset . Effects . Insert ( index , imageEffect ) ;
}
2013-11-03 23:53:49 +13:00
}
else
{
lvEffects . Items . Add ( lvi ) ;
2017-09-25 19:58:16 +13:00
if ( preset ! = null )
{
preset . Effects . Add ( imageEffect ) ;
}
2013-11-03 23:53:49 +13:00
}
lvi . EnsureVisible ( ) ;
lvi . Selected = true ;
}
2017-09-24 00:07:18 +12:00
private void LoadPreset ( ImageEffectPreset preset )
2013-11-03 23:53:49 +13:00
{
2017-09-25 20:49:08 +13:00
txtPresetName . Text = preset . Name ;
lvEffects . Items . Clear ( ) ;
2020-07-09 23:12:17 +12:00
ClearSelectedEffect ( ) ;
2017-09-24 00:48:28 +12:00
2017-09-25 20:49:08 +13:00
foreach ( ImageEffect imageEffect in preset . Effects )
2013-11-03 23:53:49 +13:00
{
2017-09-25 20:49:08 +13:00
AddEffect ( imageEffect ) ;
2013-11-03 23:53:49 +13:00
}
2017-09-24 01:17:21 +12:00
UpdatePreview ( ) ;
2013-11-03 23:53:49 +13:00
}
#region Form events
2017-09-25 20:49:08 +13:00
private void ImageEffectsForm_Shown ( object sender , EventArgs e )
{
this . ForceActivate ( ) ;
2020-07-09 01:12:43 +12:00
pauseUpdate = false ;
2020-07-09 01:54:29 +12:00
lvPresets . EnsureSelectedVisible ( ) ;
2020-07-09 01:12:43 +12:00
UpdatePreview ( ) ;
2017-09-25 20:49:08 +13:00
}
2020-07-08 23:14:24 +12:00
private void btnPresetNew_Click ( object sender , EventArgs e )
2017-09-24 00:07:18 +12:00
{
AddPreset ( ) ;
}
2020-07-08 23:14:24 +12:00
private void btnPresetRemove_Click ( object sender , EventArgs e )
2017-09-24 00:07:18 +12:00
{
2020-07-08 23:14:24 +12:00
int selected = lvPresets . SelectedIndex ;
2017-09-24 00:07:18 +12:00
if ( selected > - 1 )
{
2020-07-08 23:14:24 +12:00
lvPresets . Items . RemoveAt ( selected ) ;
2017-09-24 00:07:18 +12:00
Presets . RemoveAt ( selected ) ;
2020-07-08 23:14:24 +12:00
if ( lvPresets . Items . Count > 0 )
2017-09-24 00:07:18 +12:00
{
2020-07-08 23:14:24 +12:00
lvPresets . SelectedIndex = selected = = lvPresets . Items . Count ? lvPresets . Items . Count - 1 : selected ;
2017-09-24 00:07:18 +12:00
}
else
{
ClearFields ( ) ;
2020-07-08 23:14:24 +12:00
btnPresetNew . Focus ( ) ;
2017-09-24 00:07:18 +12:00
}
}
}
2020-07-08 23:14:24 +12:00
private void btnPresetDuplicate_Click ( object sender , EventArgs e )
2020-06-07 12:52:02 +12:00
{
ImageEffectPreset preset = GetSelectedPreset ( ) ;
if ( preset ! = null )
{
ImageEffectPreset presetClone = preset . Copy ( ) ;
AddPreset ( presetClone ) ;
}
}
2020-07-09 20:50:40 +12:00
private void lvPresets_ItemMoving ( object sender , int oldIndex , int newIndex )
{
Presets . Move ( oldIndex , newIndex ) ;
}
2020-07-08 23:14:24 +12:00
private void lvPresets_SelectedIndexChanged ( object sender , EventArgs e )
2017-09-24 00:07:18 +12:00
{
2020-07-08 23:14:24 +12:00
SelectedPresetIndex = lvPresets . SelectedIndex ;
2020-07-06 11:23:59 +12:00
2020-07-09 01:12:43 +12:00
ImageEffectPreset preset = GetSelectedPreset ( ) ;
if ( preset ! = null )
2017-09-24 00:07:18 +12:00
{
2020-07-09 01:12:43 +12:00
LoadPreset ( preset ) ;
2017-09-24 00:07:18 +12:00
}
}
private void txtPresetName_TextChanged ( object sender , EventArgs e )
{
2020-07-08 23:14:24 +12:00
ListViewItem lvi ;
ImageEffectPreset preset = GetSelectedPreset ( out lvi ) ;
2017-09-24 00:07:18 +12:00
if ( preset ! = null )
{
preset . Name = txtPresetName . Text ;
2020-07-08 23:14:24 +12:00
lvi . Text = preset . ToString ( ) ;
2017-09-24 00:07:18 +12:00
}
}
2020-07-08 23:14:24 +12:00
private void btnEffectAdd_Click ( object sender , EventArgs e )
2013-11-03 23:53:49 +13:00
{
2020-07-08 23:14:24 +12:00
cmsEffects . Show ( btnEffectAdd , 0 , btnEffectAdd . Height + 1 ) ;
2013-11-03 23:53:49 +13:00
}
2020-07-08 23:14:24 +12:00
private void btnEffectRemove_Click ( object sender , EventArgs e )
2013-11-03 23:53:49 +13:00
{
RemoveSelectedEffects ( ) ;
}
2020-07-08 23:14:24 +12:00
private void btnEffectDuplicate_Click ( object sender , EventArgs e )
2013-11-03 23:53:49 +13:00
{
2017-09-24 00:07:18 +12:00
ImageEffectPreset preset = GetSelectedPreset ( ) ;
2013-11-03 23:53:49 +13:00
2017-09-24 00:07:18 +12:00
if ( preset ! = null )
{
if ( lvEffects . SelectedItems . Count > 0 )
2013-11-03 23:53:49 +13:00
{
2017-09-24 00:07:18 +12:00
ListViewItem lvi = lvEffects . SelectedItems [ 0 ] ;
if ( lvi . Tag is ImageEffect )
{
ImageEffect imageEffect = ( ImageEffect ) lvi . Tag ;
ImageEffect imageEffectClone = imageEffect . Copy ( ) ;
2017-09-25 19:58:16 +13:00
AddEffect ( imageEffectClone , preset ) ;
2017-09-24 00:07:18 +12:00
UpdatePreview ( ) ;
}
2013-11-03 23:53:49 +13:00
}
}
}
2020-07-08 23:14:24 +12:00
private void btnEffectClear_Click ( object sender , EventArgs e )
2013-11-03 23:53:49 +13:00
{
2020-07-27 23:01:57 +12:00
// TODO: Translate
if ( MessageBox . Show ( "Would you like to clear effects?" , "ShareX - " + "Confirmation" , MessageBoxButtons . YesNo , MessageBoxIcon . Information ) = = DialogResult . Yes )
2017-09-25 19:58:16 +13:00
{
2020-07-27 23:01:57 +12:00
ImageEffectPreset preset = GetSelectedPreset ( ) ;
if ( preset ! = null )
{
lvEffects . Items . Clear ( ) ;
preset . Effects . Clear ( ) ;
ClearSelectedEffect ( ) ;
UpdatePreview ( ) ;
}
2017-09-25 19:58:16 +13:00
}
2013-11-03 23:53:49 +13:00
}
2020-07-08 23:14:24 +12:00
private void btnEffectRefresh_Click ( object sender , EventArgs e )
2019-06-26 03:46:43 +12:00
{
UpdatePreview ( ) ;
}
2018-01-21 23:23:45 +13:00
private void lvEffects_ItemMoved ( object sender , int oldIndex , int newIndex )
2013-11-03 23:53:49 +13:00
{
2018-01-21 23:23:45 +13:00
ImageEffectPreset preset = GetSelectedPreset ( ) ;
if ( preset ! = null )
{
preset . Effects . Move ( oldIndex , newIndex ) ;
UpdatePreview ( ) ;
}
2013-11-03 23:53:49 +13:00
}
private void lvEffects_SelectedIndexChanged ( object sender , EventArgs e )
{
2020-07-09 23:12:17 +12:00
ClearSelectedEffect ( ) ;
2013-11-03 23:53:49 +13:00
if ( lvEffects . SelectedItems . Count > 0 )
{
ListViewItem lvi = lvEffects . SelectedItems [ 0 ] ;
2020-07-09 23:12:17 +12:00
if ( lvi . Tag is ImageEffect ie )
2013-11-03 23:53:49 +13:00
{
2020-07-09 23:12:17 +12:00
lblEffect . Text = ie . GetType ( ) . GetDescription ( ) + ":" ;
pgSettings . SelectedObject = ie ;
2013-11-03 23:53:49 +13:00
}
}
2017-09-27 23:42:20 +13:00
UpdateControlStates ( ) ;
2013-11-03 23:53:49 +13:00
}
2014-10-20 03:02:09 +13:00
private void lvEffects_ItemChecked ( object sender , ItemCheckedEventArgs e )
{
2020-07-06 11:23:59 +12:00
if ( e . Item ! = null & & e . Item . Focused & & e . Item . Tag is ImageEffect )
2014-10-20 03:02:09 +13:00
{
ImageEffect imageEffect = ( ImageEffect ) e . Item . Tag ;
imageEffect . Enabled = e . Item . Checked ;
UpdatePreview ( ) ;
}
}
2013-11-03 23:53:49 +13:00
private void lvEffects_KeyDown ( object sender , KeyEventArgs e )
{
switch ( e . KeyData )
{
case Keys . Delete :
RemoveSelectedEffects ( ) ;
e . SuppressKeyPress = true ;
break ;
case Keys . F5 :
UpdatePreview ( ) ;
e . SuppressKeyPress = true ;
break ;
}
}
private void pgSettings_PropertyValueChanged ( object s , PropertyValueChangedEventArgs e )
{
UpdatePreview ( ) ;
}
2014-06-07 06:35:18 +12:00
private object eiImageEffects_ExportRequested ( )
2013-11-12 17:07:26 +13:00
{
2017-12-16 05:12:07 +13:00
return GetSelectedPreset ( ) ;
2013-11-12 17:07:26 +13:00
}
2014-06-07 06:35:18 +12:00
private void eiImageEffects_ImportRequested ( object obj )
2013-11-12 17:07:26 +13:00
{
2017-09-24 00:07:18 +12:00
ImageEffectPreset preset = obj as ImageEffectPreset ;
2014-06-07 06:35:18 +12:00
2017-09-24 00:07:18 +12:00
if ( preset ! = null & & preset . Effects . Count > 0 )
2013-11-12 17:07:26 +13:00
{
2017-09-24 00:07:18 +12:00
AddPreset ( preset ) ;
2013-11-12 17:07:26 +13:00
}
}
2020-07-06 11:23:59 +12:00
private void btnPackager_Click ( object sender , EventArgs e )
{
ImageEffectPreset preset = GetSelectedPreset ( ) ;
if ( preset ! = null )
{
if ( string . IsNullOrEmpty ( preset . Name ) )
{
// TODO: Translate
MessageBox . Show ( "Preset name cannot be empty." , "ShareX - " + "Missing preset name" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
}
else
{
string json = eiImageEffects . Serialize ( preset ) ;
if ( ! string . IsNullOrEmpty ( json ) )
{
using ( ImageEffectPackagerForm packagerForm = new ImageEffectPackagerForm ( json , preset . Name ,
HelpersOptions . ShareXSpecialFolders [ "ShareXImageEffects" ] ) )
{
packagerForm . ShowDialog ( ) ;
}
}
}
}
}
2015-03-31 19:09:34 +13:00
private void tsmiLoadImageFromFile_Click ( object sender , EventArgs e )
2013-11-06 06:58:22 +13:00
{
2013-11-21 06:45:11 +13:00
string filePath = ImageHelpers . OpenImageFileDialog ( ) ;
2013-11-12 19:35:44 +13:00
2013-11-21 06:45:11 +13:00
if ( ! string . IsNullOrEmpty ( filePath ) )
{
2019-09-23 20:16:52 +12:00
if ( PreviewImage ! = null ) PreviewImage . Dispose ( ) ;
2020-03-22 12:22:43 +13:00
PreviewImage = ImageHelpers . LoadImage ( filePath ) ;
2019-11-05 23:56:08 +13:00
FilePath = filePath ;
2013-11-21 06:45:11 +13:00
UpdatePreview ( ) ;
2013-11-06 06:58:22 +13:00
}
}
2015-03-31 19:09:34 +13:00
private void tsmiLoadImageFromClipboard_Click ( object sender , EventArgs e )
{
2020-03-24 08:33:26 +13:00
Bitmap bmp = ClipboardHelpers . GetImage ( ) ;
2015-03-31 19:09:34 +13:00
2020-03-21 08:02:26 +13:00
if ( bmp ! = null )
2015-03-31 19:09:34 +13:00
{
2019-09-23 20:16:52 +12:00
if ( PreviewImage ! = null ) PreviewImage . Dispose ( ) ;
2020-03-21 08:02:26 +13:00
PreviewImage = bmp ;
2019-11-05 23:56:08 +13:00
FilePath = null ;
2015-03-31 19:09:34 +13:00
UpdatePreview ( ) ;
}
}
2013-11-08 14:14:04 +13:00
private void btnSaveImage_Click ( object sender , EventArgs e )
2013-11-03 23:53:49 +13:00
{
2019-09-23 20:16:52 +12:00
if ( PreviewImage ! = null )
2013-11-08 14:14:04 +13:00
{
2015-03-31 19:09:34 +13:00
using ( Image img = ApplyEffects ( ) )
{
2016-03-18 22:00:51 +13:00
if ( img ! = null )
{
2019-11-05 23:56:08 +13:00
string filePath = ImageHelpers . SaveImageFileDialog ( img , FilePath ) ;
if ( ! string . IsNullOrEmpty ( filePath ) )
{
FilePath = filePath ;
}
2016-03-18 22:00:51 +13:00
}
2015-03-31 19:09:34 +13:00
}
2013-11-08 14:14:04 +13:00
}
2013-11-03 23:53:49 +13:00
}
2018-05-09 10:34:19 +12:00
private void btnUploadImage_Click ( object sender , EventArgs e )
{
2019-09-23 20:16:52 +12:00
if ( PreviewImage ! = null )
2018-05-09 10:34:19 +12:00
{
2020-03-24 08:33:26 +13:00
Bitmap bmp = ApplyEffects ( ) ;
2018-05-09 10:34:19 +12:00
2020-03-24 08:33:26 +13:00
if ( bmp ! = null )
2018-05-09 10:34:19 +12:00
{
2020-03-24 08:33:26 +13:00
OnImageProcessRequested ( bmp ) ;
2018-05-09 10:34:19 +12:00
}
}
}
2013-11-12 19:35:44 +13:00
private void pbResult_DragEnter ( object sender , DragEventArgs e )
{
if ( e . Data . GetDataPresent ( DataFormats . FileDrop , false ) | | e . Data . GetDataPresent ( DataFormats . Bitmap , false ) )
{
e . Effect = DragDropEffects . Copy ;
}
else
{
e . Effect = DragDropEffects . None ;
}
}
private void pbResult_DragDrop ( object sender , DragEventArgs e )
{
if ( e . Data . GetDataPresent ( DataFormats . FileDrop , false ) )
{
string [ ] files = e . Data . GetData ( DataFormats . FileDrop , false ) as string [ ] ;
if ( files ! = null & & files . Length > 0 )
{
if ( Helpers . IsImageFile ( files [ 0 ] ) )
{
2019-09-23 20:16:52 +12:00
if ( PreviewImage ! = null ) PreviewImage . Dispose ( ) ;
2020-03-22 12:22:43 +13:00
PreviewImage = ImageHelpers . LoadImage ( files [ 0 ] ) ;
2013-11-12 19:35:44 +13:00
UpdatePreview ( ) ;
}
}
}
else if ( e . Data . GetDataPresent ( DataFormats . Bitmap , false ) )
{
2020-03-21 08:02:26 +13:00
Bitmap bmp = e . Data . GetData ( DataFormats . Bitmap , false ) as Bitmap ;
2013-11-12 19:35:44 +13:00
2020-03-21 08:02:26 +13:00
if ( bmp ! = null )
2013-11-12 19:35:44 +13:00
{
2019-09-23 20:16:52 +12:00
if ( PreviewImage ! = null ) PreviewImage . Dispose ( ) ;
2020-03-21 08:02:26 +13:00
PreviewImage = bmp ;
2013-11-12 19:35:44 +13:00
UpdatePreview ( ) ;
}
}
}
2018-01-21 11:40:38 +13:00
private void btnOK_Click ( object sender , EventArgs e )
{
DialogResult = DialogResult . OK ;
Close ( ) ;
}
2017-09-27 23:42:20 +13:00
private void btnClose_Click ( object sender , EventArgs e )
2013-11-03 23:53:49 +13:00
{
Close ( ) ;
}
#endregion Form events
}
}