2013-11-03 23:53:49 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2020-02-05 20:19:48 +13:00
|
|
|
|
Copyright (c) 2007-2020 ShareX Team
|
2013-11-03 23:53:49 +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)
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
using ShareX.HelpersLib;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
2016-05-15 19:12:53 +12:00
|
|
|
|
using System.Windows.Forms;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.ScreenCaptureLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-09-05 22:31:49 +12:00
|
|
|
|
public static class RegionCaptureTasks
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2020-03-21 14:41:34 +13:00
|
|
|
|
public static Bitmap GetRegionImage(RegionCaptureOptions options)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2017-10-27 12:25:57 +13:00
|
|
|
|
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2017-10-27 12:25:57 +13:00
|
|
|
|
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.Default, newOptions))
|
|
|
|
|
{
|
2016-05-15 19:34:02 +12:00
|
|
|
|
form.ShowDialog();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2016-05-15 19:34:02 +12:00
|
|
|
|
return form.GetResultImage();
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-15 19:12:53 +12:00
|
|
|
|
|
2016-08-23 07:39:38 +12:00
|
|
|
|
public static bool GetRectangleRegion(out Rectangle rect, RegionCaptureOptions options)
|
2016-05-15 19:12:53 +12:00
|
|
|
|
{
|
2017-10-27 12:25:57 +13:00
|
|
|
|
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
|
2016-05-15 19:12:53 +12:00
|
|
|
|
|
2017-10-27 12:25:57 +13:00
|
|
|
|
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.Default, newOptions))
|
|
|
|
|
{
|
2016-05-15 19:12:53 +12:00
|
|
|
|
form.ShowDialog();
|
|
|
|
|
|
|
|
|
|
if (form.Result == RegionResult.Region)
|
|
|
|
|
{
|
2016-08-09 10:05:01 +12:00
|
|
|
|
if (form.ShapeManager.IsCurrentShapeValid)
|
2016-05-15 19:12:53 +12:00
|
|
|
|
{
|
|
|
|
|
rect = CaptureHelpers.ClientToScreen(form.ShapeManager.CurrentRectangle);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (form.Result == RegionResult.Fullscreen)
|
|
|
|
|
{
|
|
|
|
|
rect = CaptureHelpers.GetScreenBounds();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else if (form.Result == RegionResult.Monitor)
|
|
|
|
|
{
|
|
|
|
|
Screen[] screens = Screen.AllScreens;
|
|
|
|
|
|
|
|
|
|
if (form.MonitorIndex < screens.Length)
|
|
|
|
|
{
|
|
|
|
|
Screen screen = screens[form.MonitorIndex];
|
|
|
|
|
rect = screen.Bounds;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (form.Result == RegionResult.ActiveMonitor)
|
|
|
|
|
{
|
|
|
|
|
rect = CaptureHelpers.GetActiveScreenBounds();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rect = Rectangle.Empty;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-05-15 19:34:02 +12:00
|
|
|
|
|
2020-03-27 12:54:20 +13:00
|
|
|
|
public static bool GetRectangleRegionTransparent(out Rectangle rect)
|
|
|
|
|
{
|
|
|
|
|
using (RegionCaptureTransparentForm regionCaptureTransparentForm = new RegionCaptureTransparentForm())
|
|
|
|
|
{
|
|
|
|
|
if (regionCaptureTransparentForm.ShowDialog() == DialogResult.OK)
|
|
|
|
|
{
|
|
|
|
|
rect = regionCaptureTransparentForm.SelectionRectangle;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rect = Rectangle.Empty;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
public static PointInfo GetPointInfo(RegionCaptureOptions options, Bitmap canvas = null)
|
2016-05-15 19:34:02 +12:00
|
|
|
|
{
|
2017-10-27 12:25:57 +13:00
|
|
|
|
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
|
|
|
|
|
newOptions.DetectWindows = false;
|
|
|
|
|
newOptions.UseDimming = false;
|
2016-05-15 19:34:02 +12:00
|
|
|
|
|
2020-01-04 04:09:21 +13:00
|
|
|
|
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.ScreenColorPicker, newOptions, canvas))
|
2017-10-27 12:25:57 +13:00
|
|
|
|
{
|
2016-05-15 19:34:02 +12:00
|
|
|
|
form.ShowDialog();
|
|
|
|
|
|
|
|
|
|
if (form.Result == RegionResult.Region)
|
|
|
|
|
{
|
|
|
|
|
PointInfo pointInfo = new PointInfo();
|
|
|
|
|
pointInfo.Position = form.CurrentPosition;
|
2020-03-12 01:19:04 +13:00
|
|
|
|
pointInfo.Color = form.ShapeManager.GetCurrentColor();
|
2016-05-15 19:34:02 +12:00
|
|
|
|
return pointInfo;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-23 07:39:38 +12:00
|
|
|
|
public static SimpleWindowInfo GetWindowInfo(RegionCaptureOptions options)
|
2016-05-15 19:34:02 +12:00
|
|
|
|
{
|
2017-10-27 12:25:57 +13:00
|
|
|
|
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
|
|
|
|
|
newOptions.UseDimming = false;
|
|
|
|
|
newOptions.ShowMagnifier = false;
|
2016-05-15 19:34:02 +12:00
|
|
|
|
|
2017-10-27 12:25:57 +13:00
|
|
|
|
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.OneClick, newOptions))
|
|
|
|
|
{
|
2016-05-15 19:34:02 +12:00
|
|
|
|
form.ShowDialog();
|
|
|
|
|
|
|
|
|
|
if (form.Result == RegionResult.Region)
|
|
|
|
|
{
|
|
|
|
|
return form.SelectedWindow;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 23:52:12 +12:00
|
|
|
|
public static void ShowScreenColorPickerDialog(RegionCaptureOptions options)
|
2018-06-03 00:34:52 +12:00
|
|
|
|
{
|
|
|
|
|
Color color = Color.Red;
|
|
|
|
|
ColorPickerForm colorPickerForm = new ColorPickerForm(color, true);
|
|
|
|
|
colorPickerForm.EnableScreenColorPickerButton(() => GetPointInfo(options));
|
|
|
|
|
colorPickerForm.Show();
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-23 07:39:38 +12:00
|
|
|
|
public static void ShowScreenRuler(RegionCaptureOptions options)
|
2016-05-15 19:34:02 +12:00
|
|
|
|
{
|
2017-10-27 12:25:57 +13:00
|
|
|
|
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
|
|
|
|
|
newOptions.QuickCrop = false;
|
2020-07-06 10:37:56 +12:00
|
|
|
|
newOptions.UseLightResizeNodes = true;
|
2016-05-15 19:34:02 +12:00
|
|
|
|
|
2017-10-27 12:25:57 +13:00
|
|
|
|
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.Ruler, newOptions))
|
|
|
|
|
{
|
2016-05-15 19:34:02 +12:00
|
|
|
|
form.ShowDialog();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 14:41:34 +13:00
|
|
|
|
public static Bitmap ApplyRegionPathToImage(Bitmap bmp, GraphicsPath gp, out Rectangle resultArea)
|
2016-05-15 19:34:02 +12:00
|
|
|
|
{
|
2020-03-21 14:41:34 +13:00
|
|
|
|
if (bmp != null && gp != null)
|
2016-05-15 19:34:02 +12:00
|
|
|
|
{
|
2016-08-02 20:29:17 +12:00
|
|
|
|
Rectangle regionArea = Rectangle.Round(gp.GetBounds());
|
2016-05-15 19:34:02 +12:00
|
|
|
|
Rectangle screenRectangle = CaptureHelpers.GetScreenBounds0Based();
|
2018-01-14 11:17:05 +13:00
|
|
|
|
resultArea = Rectangle.Intersect(regionArea, screenRectangle);
|
2016-05-15 19:34:02 +12:00
|
|
|
|
|
2018-01-14 11:17:05 +13:00
|
|
|
|
if (resultArea.IsValid())
|
2016-05-15 19:34:02 +12:00
|
|
|
|
{
|
2020-03-21 14:41:34 +13:00
|
|
|
|
using (Bitmap bmpResult = bmp.CreateEmptyBitmap())
|
|
|
|
|
using (Graphics g = Graphics.FromImage(bmpResult))
|
|
|
|
|
using (TextureBrush brush = new TextureBrush(bmp))
|
2016-05-15 19:34:02 +12:00
|
|
|
|
{
|
2016-08-02 20:29:17 +12:00
|
|
|
|
g.PixelOffsetMode = PixelOffsetMode.Half;
|
|
|
|
|
g.SmoothingMode = SmoothingMode.HighQuality;
|
2016-05-16 21:48:53 +12:00
|
|
|
|
|
2016-08-02 20:29:17 +12:00
|
|
|
|
g.FillPath(brush, gp);
|
2016-05-15 19:34:02 +12:00
|
|
|
|
|
2020-03-21 14:41:34 +13:00
|
|
|
|
return ImageHelpers.CropBitmap(bmpResult, resultArea);
|
2016-08-02 20:29:17 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-15 19:34:02 +12:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-14 11:17:05 +13:00
|
|
|
|
resultArea = Rectangle.Empty;
|
2016-05-15 19:34:02 +12:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2016-08-23 07:39:38 +12:00
|
|
|
|
|
|
|
|
|
private static RegionCaptureOptions GetRegionCaptureOptions(RegionCaptureOptions options)
|
|
|
|
|
{
|
|
|
|
|
if (options == null)
|
|
|
|
|
{
|
|
|
|
|
return new RegionCaptureOptions();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new RegionCaptureOptions()
|
|
|
|
|
{
|
2021-01-14 09:37:12 +13:00
|
|
|
|
DetectControls = options.DetectControls,
|
2016-08-23 07:39:38 +12:00
|
|
|
|
SnapSizes = options.SnapSizes,
|
|
|
|
|
ShowMagnifier = options.ShowMagnifier,
|
|
|
|
|
UseSquareMagnifier = options.UseSquareMagnifier,
|
|
|
|
|
MagnifierPixelCount = options.MagnifierPixelCount,
|
|
|
|
|
MagnifierPixelSize = options.MagnifierPixelSize,
|
2016-09-04 23:44:01 +12:00
|
|
|
|
ShowCrosshair = options.ShowCrosshair,
|
2020-10-01 12:11:57 +13:00
|
|
|
|
AnnotationOptions = options.AnnotationOptions,
|
|
|
|
|
ScreenColorPickerInfoText = options.ScreenColorPickerInfoText
|
2016-08-23 07:39:38 +12:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|