ShareX/ShareX.ScreenCaptureLib/RegionCaptureTasks.cs

191 lines
6.7 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
2018-01-02 03:59:14 +13:00
Copyright (c) 2007-2018 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
{
public static Image GetRegionImage(RegionCaptureOptions options)
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 bool GetRectangleRegion(out Rectangle rect, RegionCaptureOptions options)
{
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.Default, newOptions))
{
form.ShowDialog();
if (form.Result == RegionResult.Region)
{
if (form.ShapeManager.IsCurrentShapeValid)
{
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;
}
public static PointInfo GetPointInfo(RegionCaptureOptions options)
{
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
newOptions.DetectWindows = false;
newOptions.UseDimming = false;
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.ScreenColorPicker, newOptions))
{
form.ShowDialog();
if (form.Result == RegionResult.Region)
{
PointInfo pointInfo = new PointInfo();
pointInfo.Position = form.CurrentPosition;
pointInfo.Color = form.CurrentColor;
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 ShowScreenRuler(RegionCaptureOptions options)
{
RegionCaptureOptions newOptions = GetRegionCaptureOptions(options);
newOptions.QuickCrop = false;
using (RegionCaptureForm form = new RegionCaptureForm(RegionCaptureMode.Ruler, newOptions))
{
form.ShowDialog();
}
}
public static Image ApplyRegionPathToImage(Image img, GraphicsPath gp, out Rectangle resultArea)
{
if (img != null && gp != null)
{
Rectangle regionArea = Rectangle.Round(gp.GetBounds());
Rectangle screenRectangle = CaptureHelpers.GetScreenBounds0Based();
resultArea = Rectangle.Intersect(regionArea, screenRectangle);
if (resultArea.IsValid())
{
using (Bitmap bmp = img.CreateEmptyBitmap())
using (Graphics g = Graphics.FromImage(bmp))
using (TextureBrush brush = new TextureBrush(img))
{
g.PixelOffsetMode = PixelOffsetMode.Half;
g.SmoothingMode = SmoothingMode.HighQuality;
2016-05-16 21:48:53 +12:00
g.FillPath(brush, gp);
return ImageHelpers.CropBitmap(bmp, resultArea);
}
}
}
resultArea = Rectangle.Empty;
return null;
}
private static RegionCaptureOptions GetRegionCaptureOptions(RegionCaptureOptions options)
{
if (options == null)
{
return new RegionCaptureOptions();
}
else
{
return new RegionCaptureOptions()
{
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
};
}
}
2013-11-03 23:53:49 +13:00
}
}