ShareX/ShareX.ScreenCaptureLib/RegionHelpers/ShapeCaptureHelpers.cs

130 lines
4.6 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
2016-01-04 04:16:01 +13:00
Copyright (c) 2007-2016 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;
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 RegionCaptureHelpers
2013-11-03 23:53:49 +13:00
{
public static Image GetRegionImage(Image backgroundImage, GraphicsPath regionFillPath, GraphicsPath regionDrawPath, SurfaceOptions options)
2013-11-03 23:53:49 +13:00
{
if (backgroundImage != null && regionFillPath != null)
2013-11-03 23:53:49 +13:00
{
Image img;
Rectangle regionArea = Rectangle.Round(regionFillPath.GetBounds());
Rectangle screenRectangle = CaptureHelpers.GetScreenBounds0Based();
Rectangle newRegionArea = Rectangle.Intersect(regionArea, screenRectangle);
using (GraphicsPath gp = (GraphicsPath)regionFillPath.Clone())
{
MoveGraphicsPath(gp, -Math.Max(0, regionArea.X), -Math.Max(0, regionArea.Y));
img = ImageHelpers.CropImage(backgroundImage, newRegionArea, gp);
2013-11-03 23:53:49 +13:00
if (options.DrawBorder)
{
GraphicsPath gpOutline = regionDrawPath ?? regionFillPath;
using (GraphicsPath gp2 = (GraphicsPath)gpOutline.Clone())
{
MoveGraphicsPath(gp2, -Math.Max(0, regionArea.X), -Math.Max(0, regionArea.Y));
img = ImageHelpers.DrawOutline(img, gp2);
}
}
}
return img;
}
return null;
}
private static void MoveGraphicsPath(GraphicsPath gp, int x, int y)
{
using (Matrix matrix = new Matrix())
{
gp.CloseFigure();
matrix.Translate(x, y);
gp.Transform(matrix);
}
}
public static bool SelectRegion(out Rectangle rect, SurfaceOptions options = null)
{
using (RectangleRegionForm form = new RectangleRegionForm(RectangleRegionMode.Default))
{
if (options != null)
{
form.Config = options;
}
form.Config.DetectWindows = true;
form.Config.QuickCrop = true;
form.Config.ShowTips = false;
form.Prepare();
form.ShowDialog();
if (form.Result == RegionResult.Region)
{
if (form.ShapeManager.IsCurrentRegionValid)
{
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;
}
2013-11-03 23:53:49 +13:00
}
}