ShareX/ShareX.ScreenCaptureLib/RegionCaptureTasks.cs

216 lines
7.8 KiB
C#
Raw Normal View History

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
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 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;
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
{
public static class RegionCaptureTasks
2013-11-03 23:53:49 +13:00
{
2022-07-27 15:27:37 +12:00
public static Bitmap GetRegionImage(RegionCaptureOptions options = null)
2013-11-03 23:53:49 +13:00
{
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
2013-11-03 23:53:49 +13:00
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.Default, newOptions))
{
form.ShowDialog();
2013-11-03 23:53:49 +13:00
return form.GetResultImage();
2013-11-03 23:53:49 +13:00
}
}
public static Bitmap GetRegionImage(out Rectangle rect, RegionCaptureOptions options = null)
{
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.Default, newOptions))
{
form.ShowDialog();
rect = form.GetSelectedRectangle();
return form.GetResultImage();
}
}
2022-08-03 03:11:38 +12:00
public static bool GetRectangleRegion(out Rectangle rect, RegionCaptureOptions options = null)
{
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.Default, newOptions))
{
form.ShowDialog();
rect = form.GetSelectedRectangle();
}
return !rect.IsEmpty;
}
public static bool GetRectangleRegion(out Rectangle rect, out WindowInfo windowInfo, RegionCaptureOptions options = null)
{
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.Default, newOptions))
{
form.ShowDialog();
rect = form.GetSelectedRectangle();
windowInfo = form.GetWindowInfo();
}
return !rect.IsEmpty;
}
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)
{
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
newOptions.DetectWindows = false;
newOptions.UseDimming = false;
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.ScreenColorPicker, newOptions, canvas))
{
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();
return pointInfo;
}
}
return null;
}
public static SimpleWindowInfo GetWindowInfo(RegionCaptureOptions options)
{
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
newOptions.UseDimming = false;
newOptions.ShowMagnifier = false;
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.OneClick, newOptions))
{
form.ShowDialog();
if (form.Result == RegionResult.Region)
{
return form.SelectedWindow;
}
}
return null;
}
public static void ShowScreenColorPickerDialog(RegionCaptureOptions options)
{
Color color = Color.Red;
ColorPickerForm colorPickerForm = new ColorPickerForm(color, true, true, options.ColorPickerOptions);
colorPickerForm.EnableScreenColorPickerButton(() => GetPointInfo(options));
colorPickerForm.Show();
}
public static void ShowScreenRuler(RegionCaptureOptions options)
{
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
newOptions.QuickCrop = false;
newOptions.UseLightResizeNodes = true;
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.Ruler, newOptions))
{
form.ShowDialog();
}
}
2020-03-21 14:41:34 +13:00
public static Bitmap ApplyRegionPathToImage(Bitmap bmp, GraphicsPath gp, out Rectangle resultArea)
{
2020-03-21 14:41:34 +13:00
if (bmp != null && gp != null)
{
Rectangle regionArea = Rectangle.Round(gp.GetBounds());
2022-02-24 22:06:13 +13:00
Rectangle screenRectangle = CaptureHelpers.GetScreenBounds();
resultArea = Rectangle.Intersect(regionArea, new Rectangle(0, 0, screenRectangle.Width, screenRectangle.Height));
if (resultArea.IsValid())
{
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))
{
g.PixelOffsetMode = PixelOffsetMode.Half;
g.SmoothingMode = SmoothingMode.HighQuality;
2016-05-16 21:48:53 +12:00
g.FillPath(brush, gp);
2020-03-21 14:41:34 +13:00
return ImageHelpers.CropBitmap(bmpResult, resultArea);
}
}
}
resultArea = Rectangle.Empty;
return null;
}
private static RegionCaptureOptions GetRegionCaptureOptions(RegionCaptureOptions options)
{
if (options == null)
{
return new RegionCaptureOptions();
}
else
{
return new RegionCaptureOptions()
{
DetectControls = options.DetectControls,
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,
AnnotationOptions = options.AnnotationOptions,
ScreenColorPickerInfoText = options.ScreenColorPickerInfoText
};
}
}
2013-11-03 23:53:49 +13:00
}
}