diff --git a/Greenshot/Drawing/CursorContainer.cs b/Greenshot/Drawing/CursorContainer.cs index d4a3ac63c..3d5c0fdb6 100644 --- a/Greenshot/Drawing/CursorContainer.cs +++ b/Greenshot/Drawing/CursorContainer.cs @@ -56,10 +56,6 @@ namespace Greenshot.Drawing { get { return cursor; } } - public override void Dispose() { - base.Dispose(); - } - // The bulk of the clean-up code is implemented in Dispose(bool) /** diff --git a/Greenshot/Drawing/Filters/AbstractFilter.cs b/Greenshot/Drawing/Filters/AbstractFilter.cs index ef47a50c0..f5c29c228 100644 --- a/Greenshot/Drawing/Filters/AbstractFilter.cs +++ b/Greenshot/Drawing/Filters/AbstractFilter.cs @@ -44,17 +44,23 @@ namespace Greenshot.Drawing.Filters { private bool invert = false; public bool Invert { - get { return invert; } - set { invert=value; OnPropertyChanged("Invert"); } + get { + return invert; + } + set { + invert = value; + OnPropertyChanged("Invert"); + } } - [NonSerialized] - protected BitmapBuffer bbb; - protected Rectangle applyRect; protected DrawableContainer parent; public DrawableContainer Parent { - get {return parent;} - set {parent = value;} + get { + return parent; + } + set { + parent = value; + } } public AbstractFilter(DrawableContainer parent) { @@ -65,36 +71,12 @@ namespace Greenshot.Drawing.Filters { return parent; } - public virtual void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { - applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); - - if (applyRect.Width == 0 || applyRect.Height == 0) { - // nothing to do - return; - } - - bbb = new BitmapBuffer(applyBitmap, applyRect); - try { - bbb.Lock(); - for(int y=0;y255) ? 255 : r; - g = (g>255) ? 255 : g; - b = (b>255) ? 255 : b; - bbb.SetColorAt(x, y, Color.FromArgb(color.A, r, g, b)); - } - - public override void Apply(Graphics graphics, Bitmap bmp, Rectangle rect, RenderMode renderMode) { - brightness = GetFieldValueAsDouble(FieldType.BRIGHTNESS); - base.Apply(graphics, bmp, rect, renderMode); + + /// + /// Implements the Apply code for the Brightness Filet + /// + /// + /// + /// + /// + public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { + Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); + + if (applyRect.Width == 0 || applyRect.Height == 0) { + // nothing to do + return; + } + + using (BitmapBuffer bbb = new BitmapBuffer(applyBitmap, applyRect)) { + bbb.Lock(); + double brightness = GetFieldValueAsDouble(FieldType.BRIGHTNESS); + for (int y = 0; y < bbb.Height; y++) { + for (int x = 0; x < bbb.Width; x++) { + if (parent.Contains(applyRect.Left + x, applyRect.Top + y) ^ Invert) { + Color color = bbb.GetColorAt(x, y); + int r = Convert.ToInt16(color.R * brightness); + int g = Convert.ToInt16(color.G * brightness); + int b = Convert.ToInt16(color.B * brightness); + r = (r > 255) ? 255 : r; + g = (g > 255) ? 255 : g; + b = (b > 255) ? 255 : b; + bbb.SetColorAt(x, y, Color.FromArgb(color.A, r, g, b)); + } + } + } + bbb.DrawTo(graphics, applyRect.Location); + } } } } diff --git a/Greenshot/Drawing/Filters/GrayscaleFilter.cs b/Greenshot/Drawing/Filters/GrayscaleFilter.cs index 09b961494..33ebd90d4 100644 --- a/Greenshot/Drawing/Filters/GrayscaleFilter.cs +++ b/Greenshot/Drawing/Filters/GrayscaleFilter.cs @@ -20,6 +20,8 @@ */ using System; using System.Drawing; +using GreenshotPlugin.Core; +using Greenshot.Plugin.Drawing; namespace Greenshot.Drawing.Filters { /// @@ -29,12 +31,29 @@ namespace Greenshot.Drawing.Filters { public class GrayscaleFilter : AbstractFilter { public GrayscaleFilter(DrawableContainer parent) : base(parent) { } - - protected override void IteratePixel(int x, int y) { - Color color = bbb.GetColorAt(x, y); - int luma = (int)((0.3*color.R) + (0.59*color.G) + (0.11*color.B)); - color = Color.FromArgb(luma, luma, luma); - bbb.SetColorAt(x, y, color); + + public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { + Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); + + if (applyRect.Width == 0 || applyRect.Height == 0) { + // nothing to do + return; + } + + using (BitmapBuffer bbb = new BitmapBuffer(applyBitmap, applyRect)) { + bbb.Lock(); + for (int y = 0; y < bbb.Height; y++) { + for (int x = 0; x < bbb.Width; x++) { + if (parent.Contains(applyRect.Left + x, applyRect.Top + y) ^ Invert) { + Color color = bbb.GetColorAt(x, y); + int luma = (int)((0.3 * color.R) + (0.59 * color.G) + (0.11 * color.B)); + color = Color.FromArgb(luma, luma, luma); + bbb.SetColorAt(x, y, color); + } + } + } + bbb.DrawTo(graphics, applyRect.Location); + } } } } diff --git a/Greenshot/Drawing/Filters/HighlightFilter.cs b/Greenshot/Drawing/Filters/HighlightFilter.cs index 9e51a7e27..9ff893dcc 100644 --- a/Greenshot/Drawing/Filters/HighlightFilter.cs +++ b/Greenshot/Drawing/Filters/HighlightFilter.cs @@ -22,26 +22,44 @@ using System; using System.Drawing; using Greenshot.Drawing.Fields; using Greenshot.Plugin.Drawing; +using GreenshotPlugin.Core; namespace Greenshot.Drawing.Filters { [Serializable()] public class HighlightFilter : AbstractFilter { - [NonSerialized] - private Color highlightColor; - public HighlightFilter(DrawableContainer parent) : base(parent) { AddField(GetType(), FieldType.FILL_COLOR, Color.Yellow); } - - protected override void IteratePixel(int x, int y) { - Color color = bbb.GetColorAt(x, y); - color = Color.FromArgb(color.A, Math.Min(highlightColor.R,color.R), Math.Min(highlightColor.G,color.G), Math.Min(highlightColor.B,color.B)); - bbb.SetColorAt(x, y, color); - } - public override void Apply(Graphics graphics, Bitmap bmp, Rectangle rect, RenderMode renderMode) { - highlightColor = GetFieldValueAsColor(FieldType.FILL_COLOR); - base.Apply(graphics, bmp, rect, renderMode); + /// + /// Implements the Apply code for the Brightness Filet + /// + /// + /// + /// + /// + public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { + Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); + + if (applyRect.Width == 0 || applyRect.Height == 0) { + // nothing to do + return; + } + + using (BitmapBuffer bbb = new BitmapBuffer(applyBitmap, applyRect)) { + bbb.Lock(); + Color highlightColor = GetFieldValueAsColor(FieldType.FILL_COLOR); + for (int y = 0; y < bbb.Height; y++) { + for (int x = 0; x < bbb.Width; x++) { + if (parent.Contains(applyRect.Left + x, applyRect.Top + y) ^ Invert) { + Color color = bbb.GetColorAt(x, y); + color = Color.FromArgb(color.A, Math.Min(highlightColor.R, color.R), Math.Min(highlightColor.G, color.G), Math.Min(highlightColor.B, color.B)); + bbb.SetColorAt(x, y, color); + } + } + } + bbb.DrawTo(graphics, applyRect.Location); + } } } } diff --git a/Greenshot/Drawing/Filters/MagnifierFilter.cs b/Greenshot/Drawing/Filters/MagnifierFilter.cs index 72d7cca1f..16a09d207 100644 --- a/Greenshot/Drawing/Filters/MagnifierFilter.cs +++ b/Greenshot/Drawing/Filters/MagnifierFilter.cs @@ -27,34 +27,37 @@ using GreenshotPlugin.Core; namespace Greenshot.Drawing.Filters { [Serializable] public class MagnifierFilter : AbstractFilter { - - [NonSerialized] - private BitmapBuffer bbbSrc; - private int magnificationFactor; - public MagnifierFilter(DrawableContainer parent) : base(parent) { AddField(GetType(), FieldType.MAGNIFICATION_FACTOR, 2); } - - public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { - magnificationFactor = GetFieldValueAsInt(FieldType.MAGNIFICATION_FACTOR); - applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); - using (bbbSrc = new BitmapBuffer(applyBitmap, applyRect)) { - bbbSrc.Lock(); - base.Apply(graphics, applyBitmap, applyRect, renderMode); + public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { + Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); + + if (applyRect.Width == 0 || applyRect.Height == 0) { + // nothing to do + return; + } + int magnificationFactor = GetFieldValueAsInt(FieldType.MAGNIFICATION_FACTOR); + + using (BitmapBuffer bbb = new BitmapBuffer(applyBitmap, applyRect)) { + int halfWidth = bbb.Size.Width / 2; + int halfHeight = bbb.Size.Height / 2; + bbb.Lock(); + using (BitmapBuffer bbbSrc = new BitmapBuffer(applyBitmap, applyRect)) { + for (int y = 0; y < bbb.Height; y++) { + int yDistanceFromCenter = halfHeight - y; + for (int x = 0; x < bbb.Width; x++) { + int xDistanceFromCenter = halfWidth - x; + if (parent.Contains(applyRect.Left + x, applyRect.Top + y) ^ Invert) { + Color color = bbbSrc.GetColorAt(halfWidth - xDistanceFromCenter / magnificationFactor, halfHeight - yDistanceFromCenter / magnificationFactor); + bbb.SetColorAt(x, y, color); + } + } + } + } + bbb.DrawTo(graphics, applyRect.Location); } - bbbSrc = null; - } - - protected override void IteratePixel(int x, int y) { - int halfWidth = bbb.Size.Width/2; - int halfHeight = bbb.Size.Height/2; - int yDistanceFromCenter = halfHeight-y; - int xDistanceFromCenter = halfWidth-x; - Color color = bbbSrc.GetColorAt(halfWidth-xDistanceFromCenter/magnificationFactor,halfHeight-yDistanceFromCenter/magnificationFactor); - bbb.SetColorAt(x, y, color); } } - } diff --git a/Greenshot/Drawing/Filters/PixelizationFilter.cs b/Greenshot/Drawing/Filters/PixelizationFilter.cs index f316ef98c..daf68e587 100644 --- a/Greenshot/Drawing/Filters/PixelizationFilter.cs +++ b/Greenshot/Drawing/Filters/PixelizationFilter.cs @@ -35,35 +35,40 @@ namespace Greenshot.Drawing.Filters { AddField(GetType(), FieldType.PIXEL_SIZE, 5); } - public static void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, int pixelSize) { - - if(pixelSize <= 1 || rect.Width == 0 || rect.Height == 0) { + public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { + int pixelSize = GetFieldValueAsInt(FieldType.PIXEL_SIZE); + Rectangle applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); + if (pixelSize <= 1 || rect.Width == 0 || rect.Height == 0) { // Nothing to do return; } - if(rect.Width < pixelSize) pixelSize = rect.Width; - if(rect.Height < pixelSize) pixelSize = rect.Height; - + if (rect.Width < pixelSize) { + pixelSize = rect.Width; + } + if (rect.Height < pixelSize) { + pixelSize = rect.Height; + } + using (BitmapBuffer bbbDest = new BitmapBuffer(applyBitmap, rect)) { bbbDest.Lock(); - using(BitmapBuffer bbbSrc = new BitmapBuffer(applyBitmap, rect)) { + using (BitmapBuffer bbbSrc = new BitmapBuffer(applyBitmap, rect)) { bbbSrc.Lock(); List colors = new List(); - int halbPixelSize = pixelSize/2; - for(int y=-halbPixelSize;y=0 && yy < bbbSrc.Height) { - for(int xx=x;xx= 0 && yy < bbbSrc.Height) { + for (int xx = x; xx < x + pixelSize; xx++) { + colors.Add(bbbSrc.GetColorAt(xx, yy)); } } } Color currentAvgColor = Colors.Mix(colors); - for(int yy=y;yy<=y+pixelSize;yy++) { - if (yy >=0 && yy < bbbSrc.Height) { - for(int xx=x;xx<=x+pixelSize;xx++) { + for (int yy = y; yy <= y + pixelSize; yy++) { + if (yy >= 0 && yy < bbbSrc.Height) { + for (int xx = x; xx <= x + pixelSize; xx++) { bbbDest.SetColorAt(xx, yy, currentAvgColor); } } @@ -74,12 +79,5 @@ namespace Greenshot.Drawing.Filters { bbbDest.DrawTo(graphics, rect.Location); } } - - public override void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode) { - int pixelSize = GetFieldValueAsInt(FieldType.PIXEL_SIZE); - applyRect = ImageHelper.CreateIntersectRectangle(applyBitmap.Size, rect, Invert); - - Apply(graphics, applyBitmap, applyRect, pixelSize); - } } } diff --git a/Greenshot/Drawing/Surface.cs b/Greenshot/Drawing/Surface.cs index 25929fc02..4606cff75 100644 --- a/Greenshot/Drawing/Surface.cs +++ b/Greenshot/Drawing/Surface.cs @@ -434,16 +434,6 @@ namespace Greenshot.Drawing { captureDetails = capture.CaptureDetails; } - /// - /// The public accessible Dispose - /// Will call the GarbageCollector to SuppressFinalize, preventing being cleaned twice - /// - public new void Dispose() { - Dispose(true); - base.Dispose(); - GC.SuppressFinalize(this); - } - protected override void Dispose(bool disposing) { if (disposing) { Count--; @@ -464,6 +454,17 @@ namespace Greenshot.Drawing { while (redoStack != null && redoStack.Count > 0) { redoStack.Pop().Dispose(); } + foreach (IDrawableContainer container in elements) { + container.Dispose(); + } + if (undrawnElement != null) { + undrawnElement.Dispose(); + undrawnElement = null; + } + if (cropContainer != null) { + cropContainer.Dispose(); + cropContainer = null; + } } base.Dispose(disposing); } @@ -909,7 +910,7 @@ namespace Greenshot.Drawing { ex.Data.Add("Width", Image.Width); ex.Data.Add("Height", Image.Height); ex.Data.Add("Pixelformat", Image.PixelFormat); - throw ex; + throw; } Point offset = new Point(-cropRectangle.Left, -cropRectangle.Top); diff --git a/Greenshot/Forms/AboutForm.cs b/Greenshot/Forms/AboutForm.cs index aa3a40476..fe5f595da 100644 --- a/Greenshot/Forms/AboutForm.cs +++ b/Greenshot/Forms/AboutForm.cs @@ -132,7 +132,12 @@ namespace Greenshot { /// public AboutForm() { // Make sure our resources are removed again. - this.Disposed += delegate { Cleanup(); }; + this.Disposed += delegate { + Cleanup(); + }; + this.FormClosing += delegate { + Cleanup(); + }; // Enable animation for this form, when we don't set this the timer doesn't start as soon as the form is loaded. EnableAnimation = true; @@ -228,6 +233,9 @@ namespace Greenshot { /// Called from the AnimatingForm, for every frame /// protected override void Animate() { + if (gBitmap == null) { + return; + } if (!isTerminalServerSession) { // Color cycle if (waitFrames != 0) { diff --git a/Greenshot/Forms/CaptureForm.cs b/Greenshot/Forms/CaptureForm.cs index 9f274b726..de17a3dc8 100644 --- a/Greenshot/Forms/CaptureForm.cs +++ b/Greenshot/Forms/CaptureForm.cs @@ -35,6 +35,7 @@ using GreenshotPlugin.UnmanagedHelpers; using GreenshotPlugin.Core; using Greenshot.IniFile; using GreenshotPlugin.Controls; +using System.Security.Permissions; namespace Greenshot.Forms { /// @@ -106,6 +107,7 @@ namespace Greenshot.Forms { /// This should prevent childs to draw backgrounds /// protected override CreateParams CreateParams { + [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x02000000; diff --git a/Greenshot/Forms/ImageEditorForm.cs b/Greenshot/Forms/ImageEditorForm.cs index e3f1ade4b..5cd7e9afc 100644 --- a/Greenshot/Forms/ImageEditorForm.cs +++ b/Greenshot/Forms/ImageEditorForm.cs @@ -657,7 +657,7 @@ namespace Greenshot { } void AboutToolStripMenuItemClick(object sender, System.EventArgs e) { - new AboutForm().ShowDialog(this); + MainForm.Instance.ShowAbout(); } void PreferencesToolStripMenuItemClick(object sender, System.EventArgs e) { diff --git a/Greenshot/Forms/MainForm.cs b/Greenshot/Forms/MainForm.cs index 3a400ef05..63b6037e8 100644 --- a/Greenshot/Forms/MainForm.cs +++ b/Greenshot/Forms/MainForm.cs @@ -968,6 +968,10 @@ namespace Greenshot { /// /// void Contextmenu_aboutClick(object sender, EventArgs e) { + ShowAbout(); + } + + public void ShowAbout() { if (aboutForm != null) { WindowDetails.ToForeground(aboutForm.Handle); } else { diff --git a/Greenshot/Helpers/IECaptureHelper.cs b/Greenshot/Helpers/IECaptureHelper.cs index f678bef9c..3173c6cc2 100644 --- a/Greenshot/Helpers/IECaptureHelper.cs +++ b/Greenshot/Helpers/IECaptureHelper.cs @@ -582,7 +582,9 @@ namespace Greenshot.Helpers { if (element.attributes.ContainsKey("greenshot") && element.attributes["greenshot"] != null) { string greenshotAction = element.attributes["greenshot"]; if ("hide".Equals(greenshotAction)) { - PixelizationFilter.Apply(graphicsTarget, returnBitmap, element.rectangle, 4); + using (Brush brush = new SolidBrush(Color.Black)) { + graphicsTarget.FillRectangle(brush, element.rectangle); + } } else if ("red".Equals(greenshotAction)) { using (Brush brush = new SolidBrush(Color.Red)) { graphicsTarget.FillRectangle(brush, element.rectangle); diff --git a/GreenshotPlugin/Core/WindowsHelper.cs b/GreenshotPlugin/Core/WindowsHelper.cs index 8a07504d8..f0dd294bb 100644 --- a/GreenshotPlugin/Core/WindowsHelper.cs +++ b/GreenshotPlugin/Core/WindowsHelper.cs @@ -159,6 +159,7 @@ namespace GreenshotPlugin.Core { /// Provides details about a Window returned by the /// enumeration /// + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1049:TypesThatOwnNativeResourcesShouldBeDisposable")] public class WindowDetails : IEquatable{ private const string METRO_WINDOWS_CLASS = "Windows.UI.Core.CoreWindow"; private const string METRO_APPLAUNCHER_CLASS = "ImmersiveLauncher"; diff --git a/GreenshotPlugin/UnmanagedHelpers/User32.cs b/GreenshotPlugin/UnmanagedHelpers/User32.cs index ce1ef4b90..45ddf0ca7 100644 --- a/GreenshotPlugin/UnmanagedHelpers/User32.cs +++ b/GreenshotPlugin/UnmanagedHelpers/User32.cs @@ -69,7 +69,7 @@ namespace GreenshotPlugin.UnmanagedHelpers { public static extern IntPtr GetWindow(IntPtr hWnd, GetWindowCommand uCmd); [DllImport("user32", SetLastError = true)] public static extern int ShowWindow(IntPtr hWnd, ShowWindowCommand nCmdShow); - [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] + [DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)] public extern static int GetWindowText(IntPtr hWnd, StringBuilder lpString, int cch); [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] public extern static int GetWindowTextLength(IntPtr hWnd); @@ -97,7 +97,7 @@ namespace GreenshotPlugin.UnmanagedHelpers { [DllImport("user32", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public extern static bool IsZoomed(IntPtr hwnd); - [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)] + [DllImport("user32", CharSet = CharSet.Unicode, SetLastError = true)] public extern static int GetClassName (IntPtr hWnd, StringBuilder lpClassName, int nMaxCount); [DllImport("user32", SetLastError = true)] public static extern IntPtr GetClassLong(IntPtr hWnd, int nIndex);