Image editor save will use last save path, save as will use last save directory, handling memory leaks

This commit is contained in:
Jaex 2014-05-04 15:31:35 +03:00
parent 4fd9b71e88
commit 39116e6dad
6 changed files with 78 additions and 242 deletions

View file

@ -46,8 +46,8 @@ public partial class ImageEditorForm : BaseForm, IImageEditor
{
public event Action<Image> ClipboardCopyRequested;
public event Action<Image> ImageUploadRequested;
public event Action<Image> ImageSaveAsRequested;
public event Action<Image> ImageSaveRequested;
public event Func<Image, string, string> ImageSaveAsRequested;
public event Action<Image, string> ImageSaveRequested;
private static EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
private static List<string> ignoreDestinations = new List<string> { };
@ -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();
}
}
}

View file

@ -227,11 +227,6 @@
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Helpers\LOG.cs" />
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="UnmanagedHelpers\DWM.cs" />
<Compile Include="UnmanagedHelpers\Enumerations.cs" />
<Compile Include="UnmanagedHelpers\GDI32.cs" />
@ -243,10 +238,6 @@
<Compile Include="UnmanagedHelpers\User32.cs" />
<Compile Include="UnmanagedHelpers\Win32Errors.cs" />
<Compile Include="UnmanagedHelpers\WinMM.cs" />
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="Forms\DropShadowSettingsForm.cs">
<SubType>Form</SubType>
</None>

View file

@ -1,63 +0,0 @@
//------------------------------------------------------------------------------
// <auto-generated>
// 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.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Greenshot.Properties {
using System;
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// 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() {
}
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[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;
}
}
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
set {
resourceCulture = value;
}
}
}
}

View file

@ -1,120 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View file

@ -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<Image> clipboardCopyRequested,
Action<Image> imageUploadRequested,
Action<Image> imageSaveRequested,
Action<Image> imageSaveAsRequested)
Action<Image, string> imageSaveRequested,
Func<Image, string, string> imageSaveAsRequested)
{
if (!IniConfig.isInitialized)
{
@ -672,7 +672,7 @@ public static Bitmap RotateImage(Image inputImage, float angleDegrees, bool upsi
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 Bitmap RotateImage(Image inputImage, float angleDegrees, bool upsi
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

View file

@ -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)