From da2c9844dade76abb5a385e0359f97ce510d3f43 Mon Sep 17 00:00:00 2001 From: Jaex Date: Sat, 21 Oct 2017 23:43:39 +0300 Subject: [PATCH] Added canvas size form --- ShareX.HelpersLib/Helpers/ImageHelpers.cs | 7 +- .../Manipulations/Canvas.cs | 27 +-- .../Forms/CanvasSizeForm.Designer.cs | 220 ++++++++++++++++++ .../Forms/CanvasSizeForm.cs | 75 ++++++ .../Forms/CanvasSizeForm.resx | 120 ++++++++++ .../Shapes/ShapeManager.cs | 24 +- .../ShareX.ScreenCaptureLib.csproj | 9 + 7 files changed, 453 insertions(+), 29 deletions(-) create mode 100644 ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.Designer.cs create mode 100644 ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.cs create mode 100644 ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.resx diff --git a/ShareX.HelpersLib/Helpers/ImageHelpers.cs b/ShareX.HelpersLib/Helpers/ImageHelpers.cs index 11d8aac5b..5f80e86cc 100644 --- a/ShareX.HelpersLib/Helpers/ImageHelpers.cs +++ b/ShareX.HelpersLib/Helpers/ImageHelpers.cs @@ -419,13 +419,16 @@ public static Bitmap QuickAutoCropImage(Bitmap bmp) return bmp; } - /// Adds empty space around image. public static Image AddCanvas(Image img, Padding margin) { + if (margin.All == 0 || img.Width + margin.Horizontal < 1 || img.Height + margin.Vertical < 1) + { + return null; + } + Bitmap bmp = img.CreateEmptyBitmap(margin.Horizontal, margin.Vertical); using (Graphics g = Graphics.FromImage(bmp)) - using (img) { g.SetHighQuality(); g.DrawImage(img, margin.Left, margin.Top, img.Width, img.Height); diff --git a/ShareX.ImageEffectsLib/Manipulations/Canvas.cs b/ShareX.ImageEffectsLib/Manipulations/Canvas.cs index d0ed5a783..eb242e216 100644 --- a/ShareX.ImageEffectsLib/Manipulations/Canvas.cs +++ b/ShareX.ImageEffectsLib/Manipulations/Canvas.cs @@ -32,23 +32,8 @@ namespace ShareX.ImageEffectsLib { internal class Canvas : ImageEffect { - private Padding margin; - [DefaultValue(typeof(Padding), "0, 0, 0, 0")] - public Padding Margin - { - get - { - return margin; - } - set - { - if (value.Top >= 0 && value.Right >= 0 && value.Bottom >= 0 && value.Left >= 0) - { - margin = value; - } - } - } + public Padding Margin { get; set; } public Canvas() { @@ -57,9 +42,15 @@ public Canvas() public override Image Apply(Image img) { - if (Margin.All == 0) return img; + Image result = ImageHelpers.AddCanvas(img, Margin); - return ImageHelpers.AddCanvas(img, Margin); + if (result == null) + { + return img; + } + + img.Dispose(); + return result; } } } \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.Designer.cs b/ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.Designer.cs new file mode 100644 index 000000000..19afc544e --- /dev/null +++ b/ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.Designer.cs @@ -0,0 +1,220 @@ +namespace ShareX.ScreenCaptureLib +{ + partial class CanvasSizeForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.nudLeft = new System.Windows.Forms.NumericUpDown(); + this.lblLeft = new System.Windows.Forms.Label(); + this.lblRight = new System.Windows.Forms.Label(); + this.nudRight = new System.Windows.Forms.NumericUpDown(); + this.lblTop = new System.Windows.Forms.Label(); + this.nudTop = new System.Windows.Forms.NumericUpDown(); + this.lblBottom = new System.Windows.Forms.Label(); + this.nudBottom = new System.Windows.Forms.NumericUpDown(); + this.btnOK = new System.Windows.Forms.Button(); + this.btnCancel = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.nudLeft)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.nudRight)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.nudTop)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.nudBottom)).BeginInit(); + this.SuspendLayout(); + // + // nudLeft + // + this.nudLeft.Location = new System.Drawing.Point(16, 80); + this.nudLeft.Maximum = new decimal(new int[] { + 10000, + 0, + 0, + 0}); + this.nudLeft.Minimum = new decimal(new int[] { + 10000, + 0, + 0, + -2147483648}); + this.nudLeft.Name = "nudLeft"; + this.nudLeft.Size = new System.Drawing.Size(64, 20); + this.nudLeft.TabIndex = 0; + this.nudLeft.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + // + // lblLeft + // + this.lblLeft.AutoSize = true; + this.lblLeft.Location = new System.Drawing.Point(13, 64); + this.lblLeft.Name = "lblLeft"; + this.lblLeft.Size = new System.Drawing.Size(28, 13); + this.lblLeft.TabIndex = 1; + this.lblLeft.Text = "Left:"; + // + // lblRight + // + this.lblRight.AutoSize = true; + this.lblRight.Location = new System.Drawing.Point(157, 64); + this.lblRight.Name = "lblRight"; + this.lblRight.Size = new System.Drawing.Size(35, 13); + this.lblRight.TabIndex = 3; + this.lblRight.Text = "Right:"; + // + // nudRight + // + this.nudRight.Location = new System.Drawing.Point(160, 80); + this.nudRight.Maximum = new decimal(new int[] { + 10000, + 0, + 0, + 0}); + this.nudRight.Minimum = new decimal(new int[] { + 10000, + 0, + 0, + -2147483648}); + this.nudRight.Name = "nudRight"; + this.nudRight.Size = new System.Drawing.Size(64, 20); + this.nudRight.TabIndex = 2; + this.nudRight.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + // + // lblTop + // + this.lblTop.AutoSize = true; + this.lblTop.Location = new System.Drawing.Point(85, 8); + this.lblTop.Name = "lblTop"; + this.lblTop.Size = new System.Drawing.Size(29, 13); + this.lblTop.TabIndex = 5; + this.lblTop.Text = "Top:"; + // + // nudTop + // + this.nudTop.Location = new System.Drawing.Point(88, 24); + this.nudTop.Maximum = new decimal(new int[] { + 10000, + 0, + 0, + 0}); + this.nudTop.Minimum = new decimal(new int[] { + 10000, + 0, + 0, + -2147483648}); + this.nudTop.Name = "nudTop"; + this.nudTop.Size = new System.Drawing.Size(64, 20); + this.nudTop.TabIndex = 4; + this.nudTop.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + // + // lblBottom + // + this.lblBottom.AutoSize = true; + this.lblBottom.Location = new System.Drawing.Point(85, 120); + this.lblBottom.Name = "lblBottom"; + this.lblBottom.Size = new System.Drawing.Size(43, 13); + this.lblBottom.TabIndex = 7; + this.lblBottom.Text = "Bottom:"; + // + // nudBottom + // + this.nudBottom.Location = new System.Drawing.Point(88, 136); + this.nudBottom.Maximum = new decimal(new int[] { + 10000, + 0, + 0, + 0}); + this.nudBottom.Minimum = new decimal(new int[] { + 10000, + 0, + 0, + -2147483648}); + this.nudBottom.Name = "nudBottom"; + this.nudBottom.Size = new System.Drawing.Size(64, 20); + this.nudBottom.TabIndex = 6; + this.nudBottom.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + // + // btnOK + // + this.btnOK.Location = new System.Drawing.Point(8, 176); + this.btnOK.Name = "btnOK"; + this.btnOK.Size = new System.Drawing.Size(108, 23); + this.btnOK.TabIndex = 8; + this.btnOK.Text = "OK"; + this.btnOK.UseVisualStyleBackColor = true; + this.btnOK.Click += new System.EventHandler(this.btnOK_Click); + // + // btnCancel + // + this.btnCancel.Location = new System.Drawing.Point(124, 176); + this.btnCancel.Name = "btnCancel"; + this.btnCancel.Size = new System.Drawing.Size(108, 23); + this.btnCancel.TabIndex = 9; + this.btnCancel.Text = "Cancel"; + this.btnCancel.UseVisualStyleBackColor = true; + this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click); + // + // CanvasSizeForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(240, 208); + this.Controls.Add(this.btnCancel); + this.Controls.Add(this.btnOK); + this.Controls.Add(this.lblBottom); + this.Controls.Add(this.nudBottom); + this.Controls.Add(this.lblTop); + this.Controls.Add(this.nudTop); + this.Controls.Add(this.lblRight); + this.Controls.Add(this.nudRight); + this.Controls.Add(this.lblLeft); + this.Controls.Add(this.nudLeft); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; + this.MaximizeBox = false; + this.MinimizeBox = false; + this.Name = "CanvasSizeForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "ShareX - Canvas size"; + this.TopMost = true; + this.Shown += new System.EventHandler(this.CanvasSizeForm_Shown); + ((System.ComponentModel.ISupportInitialize)(this.nudLeft)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.nudRight)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.nudTop)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.nudBottom)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.NumericUpDown nudLeft; + private System.Windows.Forms.Label lblLeft; + private System.Windows.Forms.Label lblRight; + private System.Windows.Forms.NumericUpDown nudRight; + private System.Windows.Forms.Label lblTop; + private System.Windows.Forms.NumericUpDown nudTop; + private System.Windows.Forms.Label lblBottom; + private System.Windows.Forms.NumericUpDown nudBottom; + private System.Windows.Forms.Button btnOK; + private System.Windows.Forms.Button btnCancel; + } +} \ No newline at end of file diff --git a/ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.cs b/ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.cs new file mode 100644 index 000000000..a48970ad3 --- /dev/null +++ b/ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.cs @@ -0,0 +1,75 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (c) 2007-2017 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 . +*/ + +#endregion License Information (GPL v3) + +using ShareX.HelpersLib; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Windows.Forms; + +namespace ShareX.ScreenCaptureLib +{ + public partial class CanvasSizeForm : Form + { + public Padding Canvas { get; private set; } + + public CanvasSizeForm() + { + InitializeComponent(); + Icon = ShareXResources.Icon; + } + + public CanvasSizeForm(Padding canvas) : this() + { + Canvas = canvas; + nudLeft.Value = canvas.Left; + nudTop.Value = canvas.Top; + nudRight.Value = canvas.Right; + nudBottom.Value = canvas.Bottom; + } + + private void CanvasSizeForm_Shown(object sender, EventArgs e) + { + this.ForceActivate(); + } + + private void btnOK_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.OK; + Canvas = new Padding((int)nudLeft.Value, (int)nudTop.Value, (int)nudRight.Value, (int)nudBottom.Value); + Close(); + } + + private void btnCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.resx b/ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.resx new file mode 100644 index 000000000..1af7de150 --- /dev/null +++ b/ShareX.ScreenCaptureLib/Forms/CanvasSizeForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs index 764d7ad4f..dc8828816 100644 --- a/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs +++ b/ShareX.ScreenCaptureLib/Shapes/ShapeManager.cs @@ -1347,19 +1347,25 @@ public Image CropImage(Rectangle rect, bool onlyIfSizeDifferent = false) private void CanvasSize() { - Padding padding = new Padding(100, 25, 50, 75); - Image img = ImageHelpers.AddCanvas(form.Image, padding); - - if (img != null) + using (CanvasSizeForm canvasSizeForm = new CanvasSizeForm()) { - Rectangle oldRect = form.ImageRectangle; + if (canvasSizeForm.ShowDialog() == DialogResult.OK) + { + Padding canvas = canvasSizeForm.Canvas; + Image img = ImageHelpers.AddCanvas(form.Image, canvas); - form.InitBackground(img); + if (img != null) + { + Rectangle oldRect = form.ImageRectangle; - MoveAll(form.ImageRectangle.X - oldRect.X + padding.Left, form.ImageRectangle.Y - oldRect.Y + padding.Top); - RemoveOutsideShapes(); + form.InitBackground(img); - isAnnotated = true; + MoveAll(form.ImageRectangle.X - oldRect.X + canvas.Left, form.ImageRectangle.Y - oldRect.Y + canvas.Top); + RemoveOutsideShapes(); + + isAnnotated = true; + } + } } } diff --git a/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj b/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj index 1b840a122..c7dc3484d 100644 --- a/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj +++ b/ShareX.ScreenCaptureLib/ShareX.ScreenCaptureLib.csproj @@ -81,6 +81,12 @@ + + Form + + + CanvasSizeForm.cs + @@ -253,6 +259,9 @@ + + CanvasSizeForm.cs + FFmpegOptionsForm.cs