ShareX/ShareX.ScreenCaptureLib/Screenshot_Transparent.cs

339 lines
12 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;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
2013-11-03 23:53:49 +13:00
using System.Threading;
using System.Windows.Forms;
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
{
2020-03-24 08:33:26 +13:00
public Bitmap CaptureWindowTransparent(IntPtr handle)
2013-11-03 23:53:49 +13:00
{
if (handle.ToInt32() > 0)
{
Rectangle rect = CaptureHelpers.GetWindowRectangle(handle);
if (CaptureShadow && !NativeMethods.IsZoomed(handle) && NativeMethods.IsDWMEnabled())
{
rect.Inflate(ShadowOffset, ShadowOffset);
Rectangle intersectBounds = Screen.AllScreens.Select(x => x.Bounds).Where(x => x.IntersectsWith(rect)).Combine();
rect.Intersect(intersectBounds);
2013-11-03 23:53:49 +13:00
}
Bitmap whiteBackground = null, blackBackground = null, whiteBackground2 = null;
2017-08-14 20:06:12 +12:00
CursorData cursorData = null;
2013-11-03 23:53:49 +13:00
bool isTransparent = false, isTaskbarHide = false;
try
{
if (AutoHideTaskbar)
{
isTaskbarHide = NativeMethods.SetTaskbarVisibilityIfIntersect(false, rect);
}
if (CaptureCursor)
{
try
{
2017-08-14 20:06:12 +12:00
cursorData = new CursorData();
2013-11-03 23:53:49 +13:00
}
catch (Exception e)
{
DebugHelper.WriteException(e, "Cursor capture failed.");
}
}
using (Form form = new Form())
{
form.BackColor = Color.White;
form.FormBorderStyle = FormBorderStyle.None;
form.ShowInTaskbar = false;
form.StartPosition = FormStartPosition.Manual;
form.Location = new Point(rect.X, rect.Y);
form.Size = new Size(rect.Width, rect.Height);
2013-11-03 23:53:49 +13:00
NativeMethods.ShowWindow(form.Handle, (int)WindowShowStyle.ShowNoActivate);
2013-11-03 23:53:49 +13:00
if (!NativeMethods.SetWindowPos(form.Handle, handle, 0, 0, 0, 0,
SetWindowPosFlags.SWP_NOMOVE | SetWindowPosFlags.SWP_NOSIZE | SetWindowPosFlags.SWP_NOACTIVATE))
2013-11-03 23:53:49 +13:00
{
form.Close();
DebugHelper.WriteLine("Transparent capture failed. Reason: SetWindowPos fail.");
return CaptureWindow(handle);
}
Application.DoEvents();
Thread.Sleep(10);
2013-11-03 23:53:49 +13:00
2020-03-21 14:41:34 +13:00
whiteBackground = CaptureRectangleNative(rect);
2013-11-03 23:53:49 +13:00
form.BackColor = Color.Black;
Application.DoEvents();
Thread.Sleep(10);
2013-11-03 23:53:49 +13:00
2020-03-21 14:41:34 +13:00
blackBackground = CaptureRectangleNative(rect);
2013-11-03 23:53:49 +13:00
form.BackColor = Color.White;
Application.DoEvents();
Thread.Sleep(10);
2013-11-03 23:53:49 +13:00
2020-03-21 14:41:34 +13:00
whiteBackground2 = CaptureRectangleNative(rect);
2013-11-03 23:53:49 +13:00
form.Close();
}
Bitmap transparentImage;
2023-03-02 09:01:00 +13:00
if (ImageHelpers.CompareImages(whiteBackground, whiteBackground2))
2013-11-03 23:53:49 +13:00
{
transparentImage = CreateTransparentImage(whiteBackground, blackBackground);
isTransparent = true;
}
else
{
DebugHelper.WriteLine("Transparent capture failed. Reason: Images not equal.");
transparentImage = whiteBackground2;
}
2018-12-30 02:32:09 +13:00
if (cursorData != null)
2013-11-03 23:53:49 +13:00
{
2018-12-30 02:32:09 +13:00
cursorData.DrawCursor(transparentImage, rect.Location);
2013-11-03 23:53:49 +13:00
}
if (isTransparent)
{
transparentImage = ImageHelpers.AutoCropImage(transparentImage);
2013-11-03 23:53:49 +13:00
if (!CaptureShadow)
{
TrimShadow(transparentImage);
}
}
return transparentImage;
}
finally
{
if (isTaskbarHide)
{
NativeMethods.SetTaskbarVisibility(true);
}
if (whiteBackground != null) whiteBackground.Dispose();
if (blackBackground != null) blackBackground.Dispose();
if (isTransparent && whiteBackground2 != null) whiteBackground2.Dispose();
}
}
return null;
}
2020-03-24 08:33:26 +13:00
public Bitmap CaptureActiveWindowTransparent()
2013-11-03 23:53:49 +13:00
{
IntPtr handle = NativeMethods.GetForegroundWindow();
return CaptureWindowTransparent(handle);
}
2016-07-22 02:23:45 +12:00
private Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground)
2013-11-03 23:53:49 +13:00
{
if (whiteBackground != null && blackBackground != null && whiteBackground.Size == blackBackground.Size)
{
Bitmap result = new Bitmap(whiteBackground.Width, whiteBackground.Height, PixelFormat.Format32bppArgb);
using (UnsafeBitmap whiteBitmap = new UnsafeBitmap(whiteBackground, true, ImageLockMode.ReadOnly))
using (UnsafeBitmap blackBitmap = new UnsafeBitmap(blackBackground, true, ImageLockMode.ReadOnly))
using (UnsafeBitmap resultBitmap = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
{
int pixelCount = blackBitmap.PixelCount;
for (int i = 0; i < pixelCount; i++)
{
ColorBgra white = whiteBitmap.GetPixel(i);
ColorBgra black = blackBitmap.GetPixel(i);
double alpha = (black.Red - white.Red + 255) / 255.0;
if (alpha == 1)
{
resultBitmap.SetPixel(i, white);
}
else if (alpha > 0)
{
white.Blue = (byte)(black.Blue / alpha);
white.Green = (byte)(black.Green / alpha);
white.Red = (byte)(black.Red / alpha);
white.Alpha = (byte)(255 * alpha);
resultBitmap.SetPixel(i, white);
}
}
}
return result;
}
return whiteBackground;
}
2016-07-22 02:23:45 +12:00
private void TrimShadow(Bitmap bitmap)
2013-11-03 23:53:49 +13:00
{
int cornerSize = 10;
int alphaOffset = Helpers.IsWindows11OrGreater() ? 50 : 200;
2013-11-03 23:53:49 +13:00
using (UnsafeBitmap unsafeBitmap = new UnsafeBitmap(bitmap, true))
{
for (int i = 0; i < cornerSize; i++)
2013-11-03 23:53:49 +13:00
{
int y = i;
int width = bitmap.Width;
// Left top
for (int x = 0; x < cornerSize; x++)
2013-11-03 23:53:49 +13:00
{
if (unsafeBitmap.GetPixel(x, y).Alpha < alphaOffset)
2013-11-03 23:53:49 +13:00
{
unsafeBitmap.ClearPixel(x, y);
}
else
{
break;
}
}
// Right top
for (int x = width - 1; x > width - cornerSize - 1; x--)
2013-11-03 23:53:49 +13:00
{
if (unsafeBitmap.GetPixel(x, y).Alpha < alphaOffset)
2013-11-03 23:53:49 +13:00
{
unsafeBitmap.ClearPixel(x, y);
}
else
{
break;
}
}
y = bitmap.Height - i - 1;
// Left bottom
for (int x = 0; x < cornerSize; x++)
2013-11-03 23:53:49 +13:00
{
if (unsafeBitmap.GetPixel(x, y).Alpha < alphaOffset)
2013-11-03 23:53:49 +13:00
{
unsafeBitmap.ClearPixel(x, y);
}
else
{
break;
}
}
// Right bottom
for (int x = width - 1; x > width - cornerSize - 1; x--)
2013-11-03 23:53:49 +13:00
{
if (unsafeBitmap.GetPixel(x, y).Alpha < alphaOffset)
2013-11-03 23:53:49 +13:00
{
unsafeBitmap.ClearPixel(x, y);
}
else
{
break;
}
}
}
}
}
#region Not in use
2016-07-22 02:23:45 +12:00
private byte[,] windows7Corner = new byte[,]
2013-11-03 23:53:49 +13:00
{
{ 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 }, { 4, 0 },
{ 0, 1 }, { 1, 1 }, { 2, 1 },
{ 0, 2 }, { 1, 2 },
{ 0, 3 },
{ 0, 4 }
};
2016-07-22 02:23:45 +12:00
private byte[,] windowsVistaCorner = new byte[,]
2013-11-03 23:53:49 +13:00
{
{ 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 },
{ 0, 1 }, { 1, 1 },
{ 0, 2 },
{ 0, 3 }
};
2016-07-22 02:23:45 +12:00
private Bitmap RemoveCorners(Image img)
2013-11-03 23:53:49 +13:00
{
byte[,] corner;
if (Helpers.IsWindows7())
{
corner = windows7Corner;
}
else if (Helpers.IsWindowsVista())
{
corner = windowsVistaCorner;
}
else
{
return null;
}
return RemoveCorners(img, corner);
}
2016-07-22 02:23:45 +12:00
private Bitmap RemoveCorners(Image img, byte[,] cornerData)
2013-11-03 23:53:49 +13:00
{
Bitmap bmp = new Bitmap(img);
for (int i = 0; i < cornerData.GetLength(0); i++)
{
// Left top corner
bmp.SetPixel(cornerData[i, 0], cornerData[i, 1], Color.Transparent);
// Right top corner
bmp.SetPixel(bmp.Width - cornerData[i, 0] - 1, cornerData[i, 1], Color.Transparent);
// Left bottom corner
bmp.SetPixel(cornerData[i, 0], bmp.Height - cornerData[i, 1] - 1, Color.Transparent);
// Right bottom corner
bmp.SetPixel(bmp.Width - cornerData[i, 0] - 1, bmp.Height - cornerData[i, 1] - 1, Color.Transparent);
}
return bmp;
}
#endregion Not in use
}
}