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;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.ScreenCaptureLib
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-07-22 02:23:45 +12:00
|
|
|
|
public partial class Screenshot
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2016-07-22 02:23:45 +12:00
|
|
|
|
public bool CaptureCursor { get; set; } = false;
|
|
|
|
|
public bool CaptureClientArea { get; set; } = false;
|
|
|
|
|
public bool RemoveOutsideScreenArea { get; set; } = true;
|
|
|
|
|
public bool CaptureShadow { get; set; } = false;
|
|
|
|
|
public int ShadowOffset { get; set; } = 20;
|
|
|
|
|
public bool AutoHideTaskbar { get; set; } = false;
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
public Bitmap CaptureRectangle(Rectangle rect)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (RemoveOutsideScreenArea)
|
|
|
|
|
{
|
|
|
|
|
Rectangle bounds = CaptureHelpers.GetScreenBounds();
|
|
|
|
|
rect = Rectangle.Intersect(bounds, rect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CaptureRectangleNative(rect, CaptureCursor);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
public Bitmap CaptureFullscreen()
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
Rectangle bounds = CaptureHelpers.GetScreenBounds();
|
|
|
|
|
|
|
|
|
|
return CaptureRectangle(bounds);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
public Bitmap CaptureWindow(IntPtr handle)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (handle.ToInt32() > 0)
|
|
|
|
|
{
|
|
|
|
|
Rectangle rect;
|
|
|
|
|
|
|
|
|
|
if (CaptureClientArea)
|
|
|
|
|
{
|
|
|
|
|
rect = NativeMethods.GetClientRect(handle);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
rect = CaptureHelpers.GetWindowRectangle(handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isTaskbarHide = false;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (AutoHideTaskbar)
|
|
|
|
|
{
|
|
|
|
|
isTaskbarHide = NativeMethods.SetTaskbarVisibilityIfIntersect(false, rect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return CaptureRectangle(rect);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (isTaskbarHide)
|
|
|
|
|
{
|
|
|
|
|
NativeMethods.SetTaskbarVisibility(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
public Bitmap CaptureActiveWindow()
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
IntPtr handle = NativeMethods.GetForegroundWindow();
|
|
|
|
|
|
|
|
|
|
return CaptureWindow(handle);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
public Bitmap CaptureActiveMonitor()
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
Rectangle bounds = CaptureHelpers.GetActiveScreenBounds();
|
|
|
|
|
|
|
|
|
|
return CaptureRectangle(bounds);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
private Bitmap CaptureRectangleNative(Rectangle rect, bool captureCursor = false)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
2020-03-21 09:22:08 +13:00
|
|
|
|
IntPtr handle = NativeMethods.GetDesktopWindow();
|
|
|
|
|
return CaptureRectangleNative(handle, rect, captureCursor);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
private Bitmap CaptureRectangleNative(IntPtr handle, Rectangle rect, bool captureCursor = false)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (rect.Width == 0 || rect.Height == 0)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IntPtr hdcSrc = NativeMethods.GetWindowDC(handle);
|
|
|
|
|
IntPtr hdcDest = NativeMethods.CreateCompatibleDC(hdcSrc);
|
|
|
|
|
IntPtr hBitmap = NativeMethods.CreateCompatibleBitmap(hdcSrc, rect.Width, rect.Height);
|
|
|
|
|
IntPtr hOld = NativeMethods.SelectObject(hdcDest, hBitmap);
|
|
|
|
|
NativeMethods.BitBlt(hdcDest, 0, 0, rect.Width, rect.Height, hdcSrc, rect.X, rect.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
|
|
|
|
|
|
|
|
|
|
if (captureCursor)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-12-30 02:32:09 +13:00
|
|
|
|
CursorData cursorData = new CursorData();
|
|
|
|
|
cursorData.DrawCursor(hdcDest, rect.Location);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
DebugHelper.WriteException(e, "Cursor capture failed.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NativeMethods.SelectObject(hdcDest, hOld);
|
|
|
|
|
NativeMethods.DeleteDC(hdcDest);
|
|
|
|
|
NativeMethods.ReleaseDC(handle, hdcSrc);
|
2020-03-21 08:02:26 +13:00
|
|
|
|
Bitmap bmp = Image.FromHbitmap(hBitmap);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
NativeMethods.DeleteObject(hBitmap);
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
return bmp;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
private Bitmap CaptureRectangleManaged(Rectangle rect)
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
if (rect.Width == 0 || rect.Height == 0)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
Bitmap bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format24bppRgb);
|
2013-11-03 23:53:49 +13:00
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
using (Graphics g = Graphics.FromImage(bmp))
|
2013-11-03 23:53:49 +13:00
|
|
|
|
{
|
|
|
|
|
// Managed can't use SourceCopy | CaptureBlt because of .NET bug
|
|
|
|
|
g.CopyFromScreen(rect.Location, Point.Empty, rect.Size, CopyPixelOperation.SourceCopy);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-21 08:02:26 +13:00
|
|
|
|
return bmp;
|
2013-11-03 23:53:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|