From 5eb0ee3e4ac13735ac5009dbecf218d3a754d338 Mon Sep 17 00:00:00 2001 From: Jaex Date: Wed, 9 Jul 2014 01:32:36 +0300 Subject: [PATCH] Experimental Rectangle Annotate capture --- HelpersLib/Extensions/NumberExtensions.cs | 2 +- HelpersLib/Helpers/ColorHelpers.cs | 2 +- HelpersLib/Helpers/Helpers.cs | 21 +- HelpersLib/Helpers/MathHelpers.cs | 19 ++ ScreenCaptureLib/Forms/RectangleAnnotate.cs | 281 ++++++++++++++++++++ ScreenCaptureLib/ScreenCaptureLib.csproj | 3 + ShareX/Forms/MainForm.Designer.cs | 28 +- 7 files changed, 320 insertions(+), 36 deletions(-) create mode 100644 ScreenCaptureLib/Forms/RectangleAnnotate.cs diff --git a/HelpersLib/Extensions/NumberExtensions.cs b/HelpersLib/Extensions/NumberExtensions.cs index 1653f3dfc..e6c3263f2 100644 --- a/HelpersLib/Extensions/NumberExtensions.cs +++ b/HelpersLib/Extensions/NumberExtensions.cs @@ -95,7 +95,7 @@ public static float Remap(this float value, float from1, float to1, float from2, public static int RandomAdd(this int num, int min, int max) { - return num + Helpers.Random(min, max); + return num + MathHelpers.Random(min, max); } private static readonly string[] Suffix_Decimal = new[] { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; diff --git a/HelpersLib/Helpers/ColorHelpers.cs b/HelpersLib/Helpers/ColorHelpers.cs index 5f69afde0..fec6e3e18 100644 --- a/HelpersLib/Helpers/ColorHelpers.cs +++ b/HelpersLib/Helpers/ColorHelpers.cs @@ -295,7 +295,7 @@ public static Color CMYKToColor(CMYK cmyk) public static Color RandomColor() { - return Color.FromArgb(Helpers.Random(255), Helpers.Random(255), Helpers.Random(255)); + return Color.FromArgb(MathHelpers.Random(255), MathHelpers.Random(255), MathHelpers.Random(255)); } public static Color ParseColor(string color) diff --git a/HelpersLib/Helpers/Helpers.cs b/HelpersLib/Helpers/Helpers.cs index 9ac631d22..9af17ad6b 100644 --- a/HelpersLib/Helpers/Helpers.cs +++ b/HelpersLib/Helpers/Helpers.cs @@ -51,27 +51,8 @@ public static class Helpers public const string URLPathCharacters = URLCharacters + "/"; // 47 public const string ValidURLCharacters = URLPathCharacters + ":?#[]@!$&'()*+,;= "; - private static readonly object randomLock = new object(); - private static readonly Random random = new Random(); - public static readonly Version OSVersion = Environment.OSVersion.Version; - public static int Random(int max) - { - lock (randomLock) - { - return random.Next(max + 1); - } - } - - public static int Random(int min, int max) - { - lock (randomLock) - { - return random.Next(min, max + 1); - } - } - public static string GetFilenameExtension(string filePath) { if (!string.IsNullOrEmpty(filePath) && filePath.Contains('.')) @@ -146,7 +127,7 @@ public static string HourTo12(int hour) public static char GetRandomChar(string chars) { - return chars[Random(chars.Length - 1)]; + return chars[MathHelpers.Random(chars.Length - 1)]; } public static string GetRandomString(string chars, int length) diff --git a/HelpersLib/Helpers/MathHelpers.cs b/HelpersLib/Helpers/MathHelpers.cs index c332858bd..cfa7c42eb 100644 --- a/HelpersLib/Helpers/MathHelpers.cs +++ b/HelpersLib/Helpers/MathHelpers.cs @@ -33,6 +33,25 @@ public static class MathHelpers public const float DegreePI = 0.01745329f; // Math.PI / 180.0 public const float TwoPI = 6.28319f; // Math.PI * 2 + private static readonly object randomLock = new object(); + private static readonly Random random = new Random(); + + public static int Random(int max) + { + lock (randomLock) + { + return random.Next(max + 1); + } + } + + public static int Random(int min, int max) + { + lock (randomLock) + { + return random.Next(min, max + 1); + } + } + public static float RadianToDegree(float radian) { return radian * RadianPI; diff --git a/ScreenCaptureLib/Forms/RectangleAnnotate.cs b/ScreenCaptureLib/Forms/RectangleAnnotate.cs new file mode 100644 index 000000000..14e8f1a92 --- /dev/null +++ b/ScreenCaptureLib/Forms/RectangleAnnotate.cs @@ -0,0 +1,281 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (C) 2007-2014 ShareX Developers + + 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 HelpersLib; +using ScreenCaptureLib.Properties; +using System; +using System.ComponentModel; +using System.Diagnostics; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.IO; +using System.Windows.Forms; + +namespace ScreenCaptureLib +{ + public class RectangleAnnotate : Form + { + public static Rectangle LastSelectionRectangle0Based { get; set; } + + public Rectangle ScreenRectangle { get; private set; } + + public Rectangle ScreenRectangle0Based + { + get + { + return new Rectangle(0, 0, ScreenRectangle.Width, ScreenRectangle.Height); + } + } + + public Rectangle SelectionRectangle { get; private set; } + + public Rectangle SelectionRectangle0Based + { + get + { + return new Rectangle(SelectionRectangle.X - ScreenRectangle.X, SelectionRectangle.Y - ScreenRectangle.Y, SelectionRectangle.Width, SelectionRectangle.Height); + } + } + + public Point CurrentMousePosition { get; private set; } + + public Point CurrentMousePosition0Based + { + get + { + return new Point(CurrentMousePosition.X - ScreenRectangle.X, CurrentMousePosition.Y - ScreenRectangle.Y); + } + } + + public Point PreviousMousePosition { get; private set; } + + public Point PreviousMousePosition0Based + { + get + { + return new Point(PreviousMousePosition.X - ScreenRectangle.X, PreviousMousePosition.Y - ScreenRectangle.Y); + } + } + + public bool ShowRectangleInfo { get; set; } + + private Timer timer; + private Image backgroundImage; + private Pen borderDotPen, borderDotPen2; + private Point positionOnClick; + private bool isMouseDown, isDrawingMode; + private Stopwatch penTimer; + private Cursor crosshairCursor; + + public RectangleAnnotate() + { + backgroundImage = Screenshot.CaptureFullscreen(); + borderDotPen = new Pen(Color.Black, 1); + borderDotPen2 = new Pen(Color.White, 1); + borderDotPen2.DashPattern = new float[] { 5, 5 }; + penTimer = Stopwatch.StartNew(); + ScreenRectangle = CaptureHelpers.GetScreenBounds(); + + InitializeComponent(); + + using (MemoryStream cursorStream = new MemoryStream(Resources.Crosshair)) + { + crosshairCursor = new Cursor(cursorStream); + } + + Cursor = crosshairCursor; + + timer = new Timer { Interval = 10 }; + timer.Tick += timer_Tick; + timer.Start(); + } + + private IContainer components = null; + + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + + if (timer != null) timer.Dispose(); + if (backgroundImage != null) backgroundImage.Dispose(); + if (borderDotPen != null) borderDotPen.Dispose(); + if (borderDotPen2 != null) borderDotPen2.Dispose(); + if (crosshairCursor != null) crosshairCursor.Dispose(); + + base.Dispose(disposing); + } + + private void InitializeComponent() + { + SuspendLayout(); + AutoScaleDimensions = new SizeF(6F, 13F); + AutoScaleMode = AutoScaleMode.Font; + StartPosition = FormStartPosition.Manual; + Bounds = ScreenRectangle; + FormBorderStyle = FormBorderStyle.None; + SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); + Text = "ShareX - Rectangle Capture Annotate"; + ShowInTaskbar = false; + TopMost = true; + Shown += RectangleLight_Shown; + KeyUp += RectangleLight_KeyUp; + MouseDown += RectangleLight_MouseDown; + MouseUp += RectangleLight_MouseUp; + ResumeLayout(false); + } + + private void RectangleLight_Shown(object sender, EventArgs e) + { + this.ShowActivate(); + } + + private void RectangleLight_KeyUp(object sender, KeyEventArgs e) + { + if (e.KeyCode == Keys.Escape) + { + Close(); + } + } + + private void RectangleLight_MouseDown(object sender, MouseEventArgs e) + { + if (e.Button == MouseButtons.Left) + { + positionOnClick = CaptureHelpers.GetCursorPosition(); + isMouseDown = true; + + if (Control.ModifierKeys == Keys.Control) + { + isDrawingMode = true; + Cursor = Cursors.Cross; + } + } + else if (isMouseDown) + { + isMouseDown = false; + isDrawingMode = false; + Refresh(); + } + else + { + Close(); + } + } + + private void RectangleLight_MouseUp(object sender, MouseEventArgs e) + { + if (isDrawingMode) + { + isMouseDown = false; + isDrawingMode = false; + Cursor = crosshairCursor; + } + else if (isMouseDown && e.Button == MouseButtons.Left) + { + if (SelectionRectangle0Based.Width > 0 && SelectionRectangle0Based.Height > 0) + { + LastSelectionRectangle0Based = SelectionRectangle0Based; + DialogResult = DialogResult.OK; + } + + Close(); + } + } + + public Image GetAreaImage() + { + Rectangle rect = SelectionRectangle0Based; + + if (rect.Width > 0 && rect.Height > 0) + { + if (rect.X == 0 && rect.Y == 0 && rect.Width == backgroundImage.Width && rect.Height == backgroundImage.Height) + { + return (Image)backgroundImage.Clone(); + } + + return ImageHelpers.CropImage(backgroundImage, rect); + } + + return null; + } + + private void timer_Tick(object sender, EventArgs e) + { + PreviousMousePosition = CurrentMousePosition; + CurrentMousePosition = CaptureHelpers.GetCursorPosition(); + SelectionRectangle = CaptureHelpers.CreateRectangle(positionOnClick.X, positionOnClick.Y, CurrentMousePosition.X, CurrentMousePosition.Y); + Refresh(); + } + + protected override void OnPaintBackground(PaintEventArgs e) + { + //base.OnPaintBackground(e); + } + + protected override void OnPaint(PaintEventArgs e) + { + Graphics g = e.Graphics; + g.InterpolationMode = InterpolationMode.NearestNeighbor; + g.SmoothingMode = SmoothingMode.HighSpeed; + + if (isDrawingMode) + { + using (Graphics gBackgroundImage = Graphics.FromImage(backgroundImage)) + using (Pen penDrawing = new Pen(Color.FromArgb(0, 255, 0), 5) { StartCap = LineCap.Round, EndCap = LineCap.Round }) + { + gBackgroundImage.DrawLine(penDrawing, PreviousMousePosition0Based, CurrentMousePosition0Based); + } + + g.DrawImage(backgroundImage, ScreenRectangle0Based); + } + else + { + g.DrawImage(backgroundImage, ScreenRectangle0Based); + + if (isMouseDown) + { + if (ShowRectangleInfo) + { + int offset = 10; + Point position = new Point(CurrentMousePosition0Based.X + offset, CurrentMousePosition0Based.Y + offset); + + using (Font font = new Font("Arial", 17, FontStyle.Bold)) + { + ImageHelpers.DrawTextWithOutline(g, string.Format("{0}, {1}\r\n{2} x {3}", SelectionRectangle.X, SelectionRectangle.Y, + SelectionRectangle.Width, SelectionRectangle.Height), position, font, Color.White, Color.Black, 3); + } + } + + g.DrawRectangleProper(borderDotPen, SelectionRectangle0Based); + borderDotPen2.DashOffset = (int)(penTimer.Elapsed.TotalMilliseconds / 100) % 10; + g.DrawRectangleProper(borderDotPen2, SelectionRectangle0Based); + } + } + } + } +} \ No newline at end of file diff --git a/ScreenCaptureLib/ScreenCaptureLib.csproj b/ScreenCaptureLib/ScreenCaptureLib.csproj index a0fba48d1..ad34ebb06 100644 --- a/ScreenCaptureLib/ScreenCaptureLib.csproj +++ b/ScreenCaptureLib/ScreenCaptureLib.csproj @@ -54,6 +54,9 @@ + + Form + diff --git a/ShareX/Forms/MainForm.Designer.cs b/ShareX/Forms/MainForm.Designer.cs index 185e8e91f..a7c7c0864 100644 --- a/ShareX/Forms/MainForm.Designer.cs +++ b/ShareX/Forms/MainForm.Designer.cs @@ -142,6 +142,7 @@ private void InitializeComponent() this.tsmiCopyFolder = new System.Windows.Forms.ToolStripMenuItem(); this.tssCopy5 = new System.Windows.Forms.ToolStripSeparator(); this.tsmiUploadSelectedFile = new System.Windows.Forms.ToolStripMenuItem(); + this.tsmiShortenSelectedURL = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiShareSelectedURL = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiShowQRCode = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiShowResponse = new System.Windows.Forms.ToolStripMenuItem(); @@ -212,7 +213,6 @@ private void InitializeComponent() this.tsmiTrayShow = new System.Windows.Forms.ToolStripMenuItem(); this.tsmiTrayExit = new System.Windows.Forms.ToolStripMenuItem(); this.ssToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); - this.tsmiShortenSelectedURL = new System.Windows.Forms.ToolStripMenuItem(); this.tsMain.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.scMain)).BeginInit(); this.scMain.Panel1.SuspendLayout(); @@ -829,7 +829,7 @@ private void InitializeComponent() // scMain.Panel2 // this.scMain.Panel2.Controls.Add(this.pbPreview); - this.scMain.Size = new System.Drawing.Size(714, 407); + this.scMain.Size = new System.Drawing.Size(664, 407); this.scMain.SplitterDistance = 335; this.scMain.SplitterWidth = 6; this.scMain.TabIndex = 1; @@ -842,11 +842,11 @@ private void InitializeComponent() | System.Windows.Forms.AnchorStyles.Right))); this.lblDragAndDropTip.BackColor = System.Drawing.Color.White; this.lblDragAndDropTip.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162))); - this.lblDragAndDropTip.ForeColor = System.Drawing.Color.LightGray; - this.lblDragAndDropTip.Location = new System.Drawing.Point(8, 92); + this.lblDragAndDropTip.ForeColor = System.Drawing.Color.Gainsboro; + this.lblDragAndDropTip.Location = new System.Drawing.Point(8, 39); this.lblDragAndDropTip.Name = "lblDragAndDropTip"; this.lblDragAndDropTip.Padding = new System.Windows.Forms.Padding(30, 20, 30, 30); - this.lblDragAndDropTip.Size = new System.Drawing.Size(319, 223); + this.lblDragAndDropTip.Size = new System.Drawing.Size(319, 328); this.lblDragAndDropTip.TabIndex = 1; this.lblDragAndDropTip.Text = "You can drag and drop files to this window"; this.lblDragAndDropTip.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; @@ -935,7 +935,7 @@ private void InitializeComponent() this.pbPreview.FullscreenOnClick = true; this.pbPreview.Location = new System.Drawing.Point(0, 0); this.pbPreview.Name = "pbPreview"; - this.pbPreview.Size = new System.Drawing.Size(373, 407); + this.pbPreview.Size = new System.Drawing.Size(323, 407); this.pbPreview.TabIndex = 0; // // cmsUploadInfo @@ -956,7 +956,7 @@ private void InitializeComponent() this.tsmiImagePreview}); this.cmsUploadInfo.Name = "cmsHistory"; this.cmsUploadInfo.ShowImageMargin = false; - this.cmsUploadInfo.Size = new System.Drawing.Size(129, 296); + this.cmsUploadInfo.Size = new System.Drawing.Size(129, 274); // // tsmiShowErrors // @@ -1238,6 +1238,12 @@ private void InitializeComponent() this.tsmiUploadSelectedFile.Text = "Upload"; this.tsmiUploadSelectedFile.Click += new System.EventHandler(this.tsmiUploadSelectedFile_Click); // + // tsmiShortenSelectedURL + // + this.tsmiShortenSelectedURL.Name = "tsmiShortenSelectedURL"; + this.tsmiShortenSelectedURL.Size = new System.Drawing.Size(128, 22); + this.tsmiShortenSelectedURL.Text = "Shorten URL"; + // // tsmiShareSelectedURL // this.tsmiShareSelectedURL.Name = "tsmiShareSelectedURL"; @@ -1823,18 +1829,12 @@ private void InitializeComponent() this.ssToolStripMenuItem.Size = new System.Drawing.Size(152, 22); this.ssToolStripMenuItem.Text = "ss"; // - // tsmiShortenSelectedURL - // - this.tsmiShortenSelectedURL.Name = "tsmiShortenSelectedURL"; - this.tsmiShortenSelectedURL.Size = new System.Drawing.Size(128, 22); - this.tsmiShortenSelectedURL.Text = "Shorten URL"; - // // MainForm // this.AllowDrop = true; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(884, 407); + this.ClientSize = new System.Drawing.Size(834, 407); this.Controls.Add(this.scMain); this.Controls.Add(this.tsMain); this.DoubleBuffered = true;