2015-04-02 00:12:21 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2022-01-12 05:32:17 +13:00
|
|
|
|
Copyright (c) 2007-2022 ShareX Team
|
2015-04-02 00:12:21 +13:00
|
|
|
|
|
|
|
|
|
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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
using ShareX.HelpersLib;
|
|
|
|
|
using ShareX.ScreenCaptureLib.Properties;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.ScreenCaptureLib
|
|
|
|
|
{
|
2016-09-05 04:22:27 +12:00
|
|
|
|
public sealed class RegionCaptureTransparentForm : LayeredForm
|
2015-04-02 00:12:21 +13:00
|
|
|
|
{
|
2016-06-12 00:18:38 +12:00
|
|
|
|
private const int MinimumRectangleSize = 3;
|
|
|
|
|
|
2022-02-24 00:15:45 +13:00
|
|
|
|
public static Rectangle LastSelectionRectangle0Based { get; private set; }
|
2015-04-02 00:12:21 +13:00
|
|
|
|
|
|
|
|
|
public Rectangle ScreenRectangle { get; private set; }
|
2016-06-12 00:18:38 +12:00
|
|
|
|
public Rectangle ScreenRectangle0Based => new Rectangle(0, 0, ScreenRectangle.Width, ScreenRectangle.Height);
|
2015-04-02 00:12:21 +13:00
|
|
|
|
public Rectangle SelectionRectangle { get; private set; }
|
2016-06-12 00:18:38 +12:00
|
|
|
|
public Rectangle SelectionRectangle0Based => new Rectangle(SelectionRectangle.X - ScreenRectangle.X,
|
|
|
|
|
SelectionRectangle.Y - ScreenRectangle.Y, SelectionRectangle.Width, SelectionRectangle.Height);
|
2015-04-04 01:25:08 +13:00
|
|
|
|
private Rectangle PreviousSelectionRectangle { get; set; }
|
2022-02-24 00:15:45 +13:00
|
|
|
|
private Rectangle PreviousSelectionRectangle0Based => new Rectangle(PreviousSelectionRectangle.X - ScreenRectangle.X,
|
|
|
|
|
PreviousSelectionRectangle.Y - ScreenRectangle.Y, PreviousSelectionRectangle.Width, PreviousSelectionRectangle.Height);
|
2015-04-02 00:12:21 +13:00
|
|
|
|
|
|
|
|
|
private Timer timer;
|
2016-05-15 19:12:53 +12:00
|
|
|
|
private Bitmap backgroundImage;
|
|
|
|
|
private Graphics gBackgroundImage;
|
2015-04-04 01:25:08 +13:00
|
|
|
|
private Pen clearPen, borderDotPen, borderDotPen2;
|
|
|
|
|
private Point currentPosition, positionOnClick;
|
2015-04-02 00:12:21 +13:00
|
|
|
|
private bool isMouseDown;
|
|
|
|
|
private Stopwatch penTimer;
|
|
|
|
|
|
2022-02-24 00:15:45 +13:00
|
|
|
|
public RegionCaptureTransparentForm(bool activeMonitorMode = false)
|
2015-04-02 00:12:21 +13:00
|
|
|
|
{
|
2015-04-04 01:25:08 +13:00
|
|
|
|
clearPen = new Pen(Color.FromArgb(1, 0, 0, 0));
|
2015-04-02 00:12:21 +13:00
|
|
|
|
borderDotPen = new Pen(Color.Black, 1);
|
|
|
|
|
borderDotPen2 = new Pen(Color.White, 1);
|
|
|
|
|
borderDotPen2.DashPattern = new float[] { 5, 5 };
|
|
|
|
|
penTimer = Stopwatch.StartNew();
|
2022-02-24 00:15:45 +13:00
|
|
|
|
|
2022-02-24 00:46:11 +13:00
|
|
|
|
if (activeMonitorMode)
|
2022-02-24 00:15:45 +13:00
|
|
|
|
{
|
|
|
|
|
ScreenRectangle = CaptureHelpers.GetActiveScreenBounds();
|
2022-02-24 00:46:11 +13:00
|
|
|
|
|
|
|
|
|
Helpers.LockCursorToWindow(this);
|
2022-02-24 00:15:45 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ScreenRectangle = CaptureHelpers.GetScreenBounds();
|
|
|
|
|
}
|
2015-04-04 01:25:08 +13:00
|
|
|
|
|
2016-05-15 19:12:53 +12:00
|
|
|
|
backgroundImage = new Bitmap(ScreenRectangle.Width, ScreenRectangle.Height);
|
|
|
|
|
gBackgroundImage = Graphics.FromImage(backgroundImage);
|
|
|
|
|
gBackgroundImage.InterpolationMode = InterpolationMode.NearestNeighbor;
|
|
|
|
|
gBackgroundImage.SmoothingMode = SmoothingMode.HighSpeed;
|
|
|
|
|
gBackgroundImage.CompositingMode = CompositingMode.SourceCopy;
|
|
|
|
|
gBackgroundImage.CompositingQuality = CompositingQuality.HighSpeed;
|
|
|
|
|
gBackgroundImage.Clear(Color.FromArgb(1, 0, 0, 0));
|
2015-04-02 00:12:21 +13:00
|
|
|
|
|
2015-04-02 12:06:09 +13:00
|
|
|
|
StartPosition = FormStartPosition.Manual;
|
|
|
|
|
Bounds = ScreenRectangle;
|
|
|
|
|
Text = "ShareX - " + Resources.RectangleTransparent_RectangleTransparent_Rectangle_capture_transparent;
|
2016-07-04 22:27:31 +12:00
|
|
|
|
Cursor = Helpers.CreateCursor(Resources.Crosshair);
|
2022-01-13 09:37:07 +13:00
|
|
|
|
TopMost = true;
|
2015-04-02 12:06:09 +13:00
|
|
|
|
|
2016-06-24 02:53:26 +12:00
|
|
|
|
Shown += RectangleTransparent_Shown;
|
|
|
|
|
KeyUp += RectangleTransparent_KeyUp;
|
|
|
|
|
MouseDown += RectangleTransparent_MouseDown;
|
|
|
|
|
MouseUp += RectangleTransparent_MouseUp;
|
2015-04-02 12:06:09 +13:00
|
|
|
|
|
2015-04-02 00:12:21 +13:00
|
|
|
|
timer = new Timer { Interval = 10 };
|
|
|
|
|
timer.Tick += timer_Tick;
|
|
|
|
|
timer.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (timer != null) timer.Dispose();
|
2015-04-04 01:25:08 +13:00
|
|
|
|
if (clearPen != null) clearPen.Dispose();
|
2015-04-02 00:12:21 +13:00
|
|
|
|
if (borderDotPen != null) borderDotPen.Dispose();
|
|
|
|
|
if (borderDotPen2 != null) borderDotPen2.Dispose();
|
2016-05-15 19:12:53 +12:00
|
|
|
|
if (gBackgroundImage != null) gBackgroundImage.Dispose();
|
|
|
|
|
if (backgroundImage != null) backgroundImage.Dispose();
|
2015-04-02 00:12:21 +13:00
|
|
|
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 02:53:26 +12:00
|
|
|
|
private void RectangleTransparent_Shown(object sender, EventArgs e)
|
2015-04-02 00:12:21 +13:00
|
|
|
|
{
|
2016-03-10 05:37:54 +13:00
|
|
|
|
this.ForceActivate();
|
2015-04-02 00:12:21 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 02:53:26 +12:00
|
|
|
|
private void RectangleTransparent_KeyUp(object sender, KeyEventArgs e)
|
2015-04-02 00:12:21 +13:00
|
|
|
|
{
|
|
|
|
|
if (e.KeyCode == Keys.Escape)
|
|
|
|
|
{
|
2016-06-12 00:18:38 +12:00
|
|
|
|
DialogResult = DialogResult.Cancel;
|
2016-06-24 02:53:26 +12:00
|
|
|
|
Close();
|
2015-04-02 00:12:21 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 02:53:26 +12:00
|
|
|
|
private void RectangleTransparent_MouseDown(object sender, MouseEventArgs e)
|
2015-04-02 00:12:21 +13:00
|
|
|
|
{
|
|
|
|
|
if (e.Button == MouseButtons.Left)
|
|
|
|
|
{
|
|
|
|
|
positionOnClick = CaptureHelpers.GetCursorPosition();
|
|
|
|
|
isMouseDown = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-24 02:53:26 +12:00
|
|
|
|
private void RectangleTransparent_MouseUp(object sender, MouseEventArgs e)
|
2015-04-02 00:12:21 +13:00
|
|
|
|
{
|
|
|
|
|
if (e.Button == MouseButtons.Left)
|
|
|
|
|
{
|
2016-06-12 00:18:38 +12:00
|
|
|
|
if (isMouseDown && SelectionRectangle0Based.Width > MinimumRectangleSize && SelectionRectangle0Based.Height > MinimumRectangleSize)
|
2015-04-02 00:12:21 +13:00
|
|
|
|
{
|
2016-06-12 00:18:38 +12:00
|
|
|
|
LastSelectionRectangle0Based = SelectionRectangle0Based;
|
|
|
|
|
DialogResult = DialogResult.OK;
|
2016-06-24 02:53:26 +12:00
|
|
|
|
Close();
|
2016-06-12 00:18:38 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
isMouseDown = false;
|
2015-04-02 00:12:21 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-24 02:53:26 +12:00
|
|
|
|
else if (e.Button == MouseButtons.Right)
|
2015-04-02 00:12:21 +13:00
|
|
|
|
{
|
|
|
|
|
if (isMouseDown)
|
|
|
|
|
{
|
|
|
|
|
isMouseDown = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-06-12 00:18:38 +12:00
|
|
|
|
DialogResult = DialogResult.Cancel;
|
2016-06-24 02:53:26 +12:00
|
|
|
|
Close();
|
2015-04-02 00:12:21 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 08:33:26 +13:00
|
|
|
|
public Bitmap GetAreaImage(Screenshot screenshot)
|
2015-04-02 00:12:21 +13:00
|
|
|
|
{
|
|
|
|
|
Rectangle rect = SelectionRectangle0Based;
|
|
|
|
|
|
|
|
|
|
if (rect.Width > 0 && rect.Height > 0)
|
|
|
|
|
{
|
2016-07-22 02:23:45 +12:00
|
|
|
|
return screenshot.CaptureRectangle(SelectionRectangle);
|
2015-04-02 00:12:21 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void timer_Tick(object sender, EventArgs e)
|
|
|
|
|
{
|
2015-04-04 01:25:08 +13:00
|
|
|
|
currentPosition = CaptureHelpers.GetCursorPosition();
|
|
|
|
|
PreviousSelectionRectangle = SelectionRectangle;
|
2022-02-24 00:15:45 +13:00
|
|
|
|
SelectionRectangle = CaptureHelpers.CreateRectangle(positionOnClick.X, positionOnClick.Y, currentPosition.X, currentPosition.Y);
|
2015-04-04 01:25:08 +13:00
|
|
|
|
|
2016-06-12 00:18:38 +12:00
|
|
|
|
UpdateBackgroundImage();
|
2015-04-02 00:12:21 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-15 19:12:53 +12:00
|
|
|
|
private void UpdateBackgroundImage()
|
2015-04-02 00:12:21 +13:00
|
|
|
|
{
|
2015-04-04 01:25:08 +13:00
|
|
|
|
// Clear previous rectangle selection
|
2016-05-15 19:12:53 +12:00
|
|
|
|
gBackgroundImage.DrawRectangleProper(clearPen, PreviousSelectionRectangle0Based);
|
2015-04-02 00:12:21 +13:00
|
|
|
|
|
2016-06-12 00:18:38 +12:00
|
|
|
|
if (isMouseDown && SelectionRectangle0Based.Width > MinimumRectangleSize && SelectionRectangle0Based.Height > MinimumRectangleSize)
|
2015-04-02 00:12:21 +13:00
|
|
|
|
{
|
2015-11-29 02:58:58 +13:00
|
|
|
|
borderDotPen2.DashOffset = (float)penTimer.Elapsed.TotalSeconds * -15;
|
2016-05-15 19:12:53 +12:00
|
|
|
|
|
|
|
|
|
gBackgroundImage.DrawRectangleProper(borderDotPen, SelectionRectangle0Based);
|
|
|
|
|
gBackgroundImage.DrawRectangleProper(borderDotPen2, SelectionRectangle0Based);
|
2015-04-02 00:12:21 +13:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-15 19:12:53 +12:00
|
|
|
|
SelectBitmap(backgroundImage);
|
2015-04-02 00:12:21 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|