From 39116e6dadd22c034dc69dd8e293b7efc75319fb Mon Sep 17 00:00:00 2001 From: Jaex Date: Sun, 4 May 2014 15:31:35 +0300 Subject: [PATCH] Image editor save will use last save path, save as will use last save directory, handling memory leaks --- GreenshotImageEditor/Forms/ImageEditorForm.cs | 91 +++++++------ .../GreenshotImageEditor.csproj | 9 -- .../Properties/Resources.Designer.cs | 63 --------- .../Properties/Resources.resx | 120 ------------------ HelpersLib/Helpers/ImageHelpers.cs | 24 +++- ShareX/TaskHelpers.cs | 13 +- 6 files changed, 78 insertions(+), 242 deletions(-) delete mode 100644 GreenshotImageEditor/Properties/Resources.Designer.cs delete mode 100644 GreenshotImageEditor/Properties/Resources.resx diff --git a/GreenshotImageEditor/Forms/ImageEditorForm.cs b/GreenshotImageEditor/Forms/ImageEditorForm.cs index 2957e8eca..4353be874 100644 --- a/GreenshotImageEditor/Forms/ImageEditorForm.cs +++ b/GreenshotImageEditor/Forms/ImageEditorForm.cs @@ -46,8 +46,8 @@ public partial class ImageEditorForm : BaseForm, IImageEditor { public event Action ClipboardCopyRequested; public event Action ImageUploadRequested; - public event Action ImageSaveAsRequested; - public event Action ImageSaveRequested; + public event Func ImageSaveAsRequested; + public event Action ImageSaveRequested; private static EditorConfiguration editorConfiguration = IniConfig.GetIniSection(); private static List ignoreDestinations = new List { }; @@ -642,7 +642,7 @@ private void ImageEditorFormActivated(object sender, EventArgs e) updateClipboardSurfaceDependencies(); updateUndoRedoSurfaceDependencies(); - saveToolStripMenuItem.Visible = File.Exists(surface.LastSaveFullPath); + saveToolStripMenuItem.Enabled = File.Exists(surface.LastSaveFullPath); } private void ImageEditorFormFormClosing(object sender, FormClosingEventArgs e) @@ -1321,6 +1321,53 @@ private void ImageEditorFormResize(object sender, EventArgs e) } } + public void OnClipboardCopyRequested() + { + if (ClipboardCopyRequested != null) + { + using (Image img = surface.GetImageForExport()) + { + ClipboardCopyRequested(img); + } + } + } + + public void OnImageUploadRequested() + { + if (ImageUploadRequested != null) + { + // Image will be disposed in upload task + Image img = surface.GetImageForExport(); + ImageUploadRequested(img); + } + } + + public void OnImageSaveRequested() + { + if (ImageSaveRequested != null && File.Exists(surface.LastSaveFullPath)) + { + using (Image img = surface.GetImageForExport()) + { + ImageSaveRequested(img, surface.LastSaveFullPath); + } + } + } + + public void OnImageSaveAsRequested() + { + if (ImageSaveAsRequested != null) + { + using (Image img = surface.GetImageForExport()) + { + string newFilePath = ImageSaveAsRequested(img, surface.LastSaveFullPath); + if (!string.IsNullOrEmpty(newFilePath)) + { + SetImagePath(newFilePath); + } + } + } + } + private void btnSaveClose_Click(object sender, EventArgs e) { OnImageSaveRequested(); @@ -1341,52 +1388,16 @@ private void btnClipboardCopy_Click(object sender, EventArgs e) OnClipboardCopyRequested(); } - public void OnClipboardCopyRequested() - { - if (ClipboardCopyRequested != null) - { - Image img = surface.GetImageForExport(); - ClipboardCopyRequested(img); - } - } - private void btnUploadImage_Click(object sender, EventArgs e) { OnImageUploadRequested(); } - public void OnImageUploadRequested() - { - if (ImageUploadRequested != null) - { - Image img = surface.GetImageForExport(); - ImageUploadRequested(img); - } - } - - public void OnImageSaveAsRequested() - { - if (ImageSaveAsRequested != null) - { - Image img = surface.GetImageForExport(); - ImageSaveAsRequested(img); - } - } - private void btnSaveAs_Click(object sender, EventArgs e) { OnImageSaveAsRequested(); } - public void OnImageSaveRequested() - { - if (File.Exists(surface.LastSaveFullPath) && ImageSaveRequested != null) - { - Image img = surface.GetImageForExport(); - ImageSaveRequested(img); - } - } - private void saveToolStripMenuItem_Click(object sender, EventArgs e) { OnImageSaveRequested(); @@ -1394,7 +1405,7 @@ private void saveToolStripMenuItem_Click(object sender, EventArgs e) private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { - btnSaveAs_Click(sender, e); + OnImageSaveAsRequested(); } } } \ No newline at end of file diff --git a/GreenshotImageEditor/GreenshotImageEditor.csproj b/GreenshotImageEditor/GreenshotImageEditor.csproj index f734955cb..3abff0335 100644 --- a/GreenshotImageEditor/GreenshotImageEditor.csproj +++ b/GreenshotImageEditor/GreenshotImageEditor.csproj @@ -227,11 +227,6 @@ - - True - True - Resources.resx - @@ -243,10 +238,6 @@ - - ResXFileCodeGenerator - Resources.Designer.cs - Form diff --git a/GreenshotImageEditor/Properties/Resources.Designer.cs b/GreenshotImageEditor/Properties/Resources.Designer.cs deleted file mode 100644 index ee96a9ee4..000000000 --- a/GreenshotImageEditor/Properties/Resources.Designer.cs +++ /dev/null @@ -1,63 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.34014 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace Greenshot.Properties { - using System; - - - /// - /// A strongly-typed resource class, for looking up localized strings, etc. - /// - // This class was auto-generated by the StronglyTypedResourceBuilder - // class via a tool like ResGen or Visual Studio. - // To add or remove a member, edit your .ResX file then rerun ResGen - // with the /str option, or rebuild your VS project. - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - internal class Resources { - - private static global::System.Resources.ResourceManager resourceMan; - - private static global::System.Globalization.CultureInfo resourceCulture; - - [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] - internal Resources() { - } - - /// - /// Returns the cached ResourceManager instance used by this class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Resources.ResourceManager ResourceManager { - get { - if (object.ReferenceEquals(resourceMan, null)) { - global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Greenshot.Properties.Resources", typeof(Resources).Assembly); - resourceMan = temp; - } - return resourceMan; - } - } - - /// - /// Overrides the current thread's CurrentUICulture property for all - /// resource lookups using this strongly typed resource class. - /// - [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] - internal static global::System.Globalization.CultureInfo Culture { - get { - return resourceCulture; - } - set { - resourceCulture = value; - } - } - } -} diff --git a/GreenshotImageEditor/Properties/Resources.resx b/GreenshotImageEditor/Properties/Resources.resx deleted file mode 100644 index 1af7de150..000000000 --- a/GreenshotImageEditor/Properties/Resources.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/HelpersLib/Helpers/ImageHelpers.cs b/HelpersLib/Helpers/ImageHelpers.cs index fca53155d..bb3c85476 100644 --- a/HelpersLib/Helpers/ImageHelpers.cs +++ b/HelpersLib/Helpers/ImageHelpers.cs @@ -663,8 +663,8 @@ public static Bitmap RotateImage(Image inputImage, float angleDegrees, bool upsi public static Image AnnotateImage(Image img, string imgPath, bool allowSave, string configPath, Action clipboardCopyRequested, Action imageUploadRequested, - Action imageSaveRequested, - Action imageSaveAsRequested) + Action imageSaveRequested, + Func imageSaveAsRequested) { if (!IniConfig.isInitialized) { @@ -672,7 +672,7 @@ public static Image AnnotateImage(Image img, string imgPath, bool allowSave, str IniConfig.Init(configPath); } - using (Image cloneImage = File.Exists(imgPath) ? ImageHelpers.LoadImage(imgPath) : (Image)img.Clone()) + using (Image cloneImage = img != null ? (Image)img.Clone() : ImageHelpers.LoadImage(imgPath)) using (ICapture capture = new Capture { Image = cloneImage }) using (Surface surface = new Surface(capture)) using (ImageEditorForm editor = new ImageEditorForm(surface, true)) @@ -683,7 +683,7 @@ public static Image AnnotateImage(Image img, string imgPath, bool allowSave, str editor.ImageSaveRequested += imageSaveRequested; editor.ImageSaveAsRequested += imageSaveAsRequested; - if (editor.ShowDialog() == DialogResult.OK) + if (editor.ShowDialog() == DialogResult.OK && img != null) { using (img) { @@ -1078,19 +1078,31 @@ public static void SaveImage(Image img, string filePath) } } - public static void SaveImageFileDialog(Image img, string filePath = "") + public static string SaveImageFileDialog(Image img, string filePath = "") { using (SaveFileDialog sfd = new SaveFileDialog()) { - if (!string.IsNullOrEmpty(filePath)) sfd.FileName = Path.GetFileNameWithoutExtension(filePath); + if (!string.IsNullOrEmpty(filePath)) + { + string folder = Path.GetDirectoryName(filePath); + if (!string.IsNullOrEmpty(folder)) + { + sfd.InitialDirectory = folder; + } + sfd.FileName = Path.GetFileNameWithoutExtension(filePath); + } + sfd.DefaultExt = ".png"; sfd.Filter = "PNG (*.png)|*.png|JPEG (*.jpg, *.jpeg, *.jpe, *.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|GIF (*.gif)|*.gif|BMP (*.bmp)|*.bmp|TIFF (*.tif, *.tiff)|*.tif;*.tiff"; if (sfd.ShowDialog() == DialogResult.OK) { SaveImage(img, sfd.FileName); + return sfd.FileName; } } + + return null; } // http://stackoverflow.com/questions/788335/why-does-image-fromfile-keep-a-file-handle-open-sometimes diff --git a/ShareX/TaskHelpers.cs b/ShareX/TaskHelpers.cs index 6fc562332..667c72e45 100644 --- a/ShareX/TaskHelpers.cs +++ b/ShareX/TaskHelpers.cs @@ -225,9 +225,9 @@ public static void ShowResultNotifications(string notificationText, TaskSettings } } - public static Image AnnotateImage(string filePath) + public static void AnnotateImage(string filePath) { - return AnnotateImage(null, filePath); + AnnotateImage(null, filePath); } public static Image AnnotateImage(Image img, string imgPath) @@ -235,8 +235,13 @@ public static Image AnnotateImage(Image img, string imgPath) return ImageHelpers.AnnotateImage(img, imgPath, !Program.IsSandbox, Program.PersonalPath, x => Program.MainForm.InvokeSafe(() => ClipboardHelpers.CopyImage(x)), x => Program.MainForm.InvokeSafe(() => UploadManager.RunImageTask(x)), - x => Program.MainForm.InvokeSafe(() => ImageHelpers.SaveImage(x, imgPath)), - x => Program.MainForm.InvokeSafe(() => ImageHelpers.SaveImageFileDialog(x, imgPath))); + (x, filePath) => Program.MainForm.InvokeSafe(() => ImageHelpers.SaveImage(x, filePath)), + (x, filePath) => + { + string newFilePath = null; + Program.MainForm.InvokeSafe(() => newFilePath = ImageHelpers.SaveImageFileDialog(x, filePath)); + return newFilePath; + }); } public static Image AddImageEffects(Image img, TaskSettings taskSettings)