2023-03-02 12:42:36 +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
|
2023-03-02 12:42:36 +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)
|
|
|
|
|
|
|
|
|
|
using ShareX.HelpersLib;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Drawing;
|
2024-04-07 02:27:15 +13:00
|
|
|
|
using System.Drawing.Drawing2D;
|
2023-03-02 12:42:36 +13:00
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.ScreenCaptureLib
|
|
|
|
|
{
|
|
|
|
|
internal class ScrollingCaptureManager : IDisposable
|
|
|
|
|
{
|
|
|
|
|
public ScrollingCaptureOptions Options { get; private set; }
|
|
|
|
|
public Bitmap Result { get; private set; }
|
|
|
|
|
public bool IsCapturing { get; private set; }
|
|
|
|
|
|
2024-04-07 02:27:15 +13:00
|
|
|
|
private Bitmap lastScreenshot;
|
|
|
|
|
private Bitmap previousScreenshot;
|
2023-03-02 12:42:36 +13:00
|
|
|
|
private bool stopRequested;
|
2024-04-11 12:47:06 +12:00
|
|
|
|
private ScrollingCaptureStatus status;
|
2023-03-03 09:07:50 +13:00
|
|
|
|
private int bestMatchCount, bestMatchIndex;
|
2023-03-02 12:42:36 +13:00
|
|
|
|
private WindowInfo selectedWindow;
|
|
|
|
|
private Rectangle selectedRectangle;
|
|
|
|
|
|
|
|
|
|
public ScrollingCaptureManager(ScrollingCaptureOptions options)
|
|
|
|
|
{
|
|
|
|
|
Options = options;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
2023-03-03 10:02:29 +13:00
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Reset(bool keepResult = false)
|
2023-03-02 12:42:36 +13:00
|
|
|
|
{
|
2024-04-07 02:27:15 +13:00
|
|
|
|
if (lastScreenshot != null)
|
2023-03-02 12:42:36 +13:00
|
|
|
|
{
|
2024-04-07 02:27:15 +13:00
|
|
|
|
lastScreenshot.Dispose();
|
|
|
|
|
lastScreenshot = null;
|
|
|
|
|
}
|
2023-03-02 12:42:36 +13:00
|
|
|
|
|
2024-04-07 02:27:15 +13:00
|
|
|
|
if (previousScreenshot != null)
|
|
|
|
|
{
|
|
|
|
|
previousScreenshot.Dispose();
|
|
|
|
|
previousScreenshot = null;
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 10:02:29 +13:00
|
|
|
|
if (!keepResult && Result != null)
|
2023-03-02 12:42:36 +13:00
|
|
|
|
{
|
|
|
|
|
Result.Dispose();
|
|
|
|
|
Result = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 12:47:06 +12:00
|
|
|
|
public async Task<ScrollingCaptureStatus> StartCapture()
|
2023-03-02 12:42:36 +13:00
|
|
|
|
{
|
2023-03-12 00:57:06 +13:00
|
|
|
|
if (!IsCapturing && selectedWindow != null && !selectedRectangle.IsEmpty)
|
2023-03-02 12:42:36 +13:00
|
|
|
|
{
|
|
|
|
|
IsCapturing = true;
|
2023-03-03 10:02:29 +13:00
|
|
|
|
stopRequested = false;
|
2024-04-11 12:47:06 +12:00
|
|
|
|
status = ScrollingCaptureStatus.Failed;
|
2023-03-03 10:02:29 +13:00
|
|
|
|
bestMatchCount = 0;
|
|
|
|
|
bestMatchIndex = 0;
|
2024-05-24 18:08:51 +12:00
|
|
|
|
int ignoreSideOffset = 50;
|
|
|
|
|
int ignoreBottomOffset = 50;
|
2023-03-03 10:02:29 +13:00
|
|
|
|
Reset();
|
2023-03-02 12:42:36 +13:00
|
|
|
|
|
2023-03-12 00:57:06 +13:00
|
|
|
|
ScrollingCaptureRegionForm regionForm = null;
|
|
|
|
|
|
2023-03-12 19:05:56 +13:00
|
|
|
|
if (Options.ShowRegion)
|
2023-03-12 00:57:06 +13:00
|
|
|
|
{
|
|
|
|
|
regionForm = new ScrollingCaptureRegionForm(selectedRectangle);
|
|
|
|
|
regionForm.Show();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-03 09:07:50 +13:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
selectedWindow.Activate();
|
2023-03-02 12:42:36 +13:00
|
|
|
|
|
2023-03-03 09:07:50 +13:00
|
|
|
|
await Task.Delay(Options.StartDelay);
|
2023-03-02 12:42:36 +13:00
|
|
|
|
|
2023-03-07 10:14:02 +13:00
|
|
|
|
if (Options.AutoScrollTop)
|
2023-03-02 12:42:36 +13:00
|
|
|
|
{
|
2023-03-07 10:14:02 +13:00
|
|
|
|
InputHelpers.SendKeyPress(VirtualKeyCode.HOME);
|
|
|
|
|
NativeMethods.SendMessage(selectedWindow.Handle, (int)WindowsMessages.VSCROLL, (int)ScrollBarCommands.SB_TOP, 0);
|
2023-03-02 12:42:36 +13:00
|
|
|
|
|
2023-03-07 10:14:02 +13:00
|
|
|
|
await Task.Delay(Options.ScrollDelay);
|
|
|
|
|
}
|
2023-03-02 12:42:36 +13:00
|
|
|
|
|
2024-04-07 02:27:15 +13:00
|
|
|
|
Screenshot screenshot = new Screenshot()
|
2023-03-07 10:14:02 +13:00
|
|
|
|
{
|
2024-04-07 02:27:15 +13:00
|
|
|
|
CaptureCursor = false
|
|
|
|
|
};
|
2023-03-02 12:42:36 +13:00
|
|
|
|
|
2024-04-07 02:27:15 +13:00
|
|
|
|
while (!stopRequested)
|
|
|
|
|
{
|
|
|
|
|
lastScreenshot = screenshot.CaptureRectangle(selectedRectangle);
|
2023-03-02 12:42:36 +13:00
|
|
|
|
|
2023-03-07 10:14:02 +13:00
|
|
|
|
if (CompareLastTwoImages())
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-08 16:23:05 +13:00
|
|
|
|
InputHelpers.SendMouseWheel(-120 * Options.ScrollAmount);
|
2023-03-03 09:07:50 +13:00
|
|
|
|
|
2023-03-07 10:14:02 +13:00
|
|
|
|
Stopwatch timer = Stopwatch.StartNew();
|
2023-03-03 09:07:50 +13:00
|
|
|
|
|
2024-04-07 02:27:15 +13:00
|
|
|
|
if (lastScreenshot != null)
|
2023-03-03 09:07:50 +13:00
|
|
|
|
{
|
2024-05-24 18:08:51 +12:00
|
|
|
|
if (Options.AutoIgnoreBottomEdge && previousScreenshot != null)
|
|
|
|
|
{
|
|
|
|
|
int autoIgnoreBottomOffset = GuessBottomEdge(previousScreenshot, lastScreenshot, ignoreSideOffset);
|
|
|
|
|
ignoreBottomOffset = Math.Max(ignoreBottomOffset, autoIgnoreBottomOffset + 50);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Bitmap newResult = await CombineImagesAsync(Result, lastScreenshot, ignoreSideOffset, ignoreBottomOffset);
|
2024-04-08 01:23:57 +12:00
|
|
|
|
|
|
|
|
|
if (newResult != null)
|
|
|
|
|
{
|
|
|
|
|
Result?.Dispose();
|
|
|
|
|
Result = newResult;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-03-03 09:07:50 +13:00
|
|
|
|
}
|
2023-03-07 10:14:02 +13:00
|
|
|
|
|
|
|
|
|
if (stopRequested)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-07 02:27:15 +13:00
|
|
|
|
if (lastScreenshot != null)
|
|
|
|
|
{
|
|
|
|
|
if (previousScreenshot != null)
|
|
|
|
|
{
|
|
|
|
|
previousScreenshot.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
previousScreenshot = lastScreenshot;
|
|
|
|
|
lastScreenshot = null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 10:14:02 +13:00
|
|
|
|
int delay = Options.ScrollDelay - (int)timer.ElapsedMilliseconds;
|
|
|
|
|
|
|
|
|
|
if (delay > 0)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(delay);
|
|
|
|
|
}
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
2023-03-03 09:07:50 +13:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2023-03-12 00:57:06 +13:00
|
|
|
|
regionForm?.Close();
|
|
|
|
|
|
2023-03-03 10:02:29 +13:00
|
|
|
|
Reset(true);
|
2023-03-03 09:07:50 +13:00
|
|
|
|
IsCapturing = false;
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-11 12:47:06 +12:00
|
|
|
|
|
|
|
|
|
return status;
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StopCapture()
|
|
|
|
|
{
|
|
|
|
|
if (IsCapturing)
|
|
|
|
|
{
|
|
|
|
|
stopRequested = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SelectWindow()
|
|
|
|
|
{
|
|
|
|
|
return RegionCaptureTasks.GetRectangleRegion(out selectedRectangle, out selectedWindow, new RegionCaptureOptions());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsScrollReachedBottom(IntPtr handle)
|
|
|
|
|
{
|
|
|
|
|
SCROLLINFO scrollInfo = new SCROLLINFO();
|
|
|
|
|
scrollInfo.cbSize = (uint)Marshal.SizeOf(scrollInfo);
|
|
|
|
|
scrollInfo.fMask = (uint)(ScrollInfoMask.SIF_RANGE | ScrollInfoMask.SIF_PAGE | ScrollInfoMask.SIF_TRACKPOS);
|
|
|
|
|
|
|
|
|
|
if (NativeMethods.GetScrollInfo(handle, (int)SBOrientation.SB_VERT, ref scrollInfo))
|
|
|
|
|
{
|
|
|
|
|
return scrollInfo.nMax == scrollInfo.nTrackPos + scrollInfo.nPage - 1;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 10:14:02 +13:00
|
|
|
|
return CompareLastTwoImages();
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 10:14:02 +13:00
|
|
|
|
private bool CompareLastTwoImages()
|
2023-03-02 12:42:36 +13:00
|
|
|
|
{
|
2024-04-07 02:27:15 +13:00
|
|
|
|
if (lastScreenshot != null && previousScreenshot != null)
|
2023-03-02 12:42:36 +13:00
|
|
|
|
{
|
2024-04-07 02:27:15 +13:00
|
|
|
|
return ImageHelpers.CompareImages(lastScreenshot, previousScreenshot);
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
|
|
|
|
|
2023-03-07 10:14:02 +13:00
|
|
|
|
return false;
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 18:08:51 +12:00
|
|
|
|
private async Task<Bitmap> CombineImagesAsync(Bitmap result, Bitmap currentImage, int ignoreSideOffset = 50, int ignoreBottomOffset = 50)
|
2023-03-02 12:42:36 +13:00
|
|
|
|
{
|
2024-05-24 18:08:51 +12:00
|
|
|
|
return await Task.Run(() => CombineImages(result, currentImage, ignoreSideOffset, ignoreBottomOffset));
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
|
|
|
|
|
2024-05-24 18:08:51 +12:00
|
|
|
|
private Bitmap CombineImages(Bitmap result, Bitmap currentImage, int ignoreSideOffset = 50, int ignoreBottomOffset = 50)
|
2023-03-02 12:42:36 +13:00
|
|
|
|
{
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
2024-04-11 12:47:06 +12:00
|
|
|
|
status = ScrollingCaptureStatus.Successful;
|
|
|
|
|
|
2023-03-02 12:42:36 +13:00
|
|
|
|
return (Bitmap)currentImage.Clone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int matchCount = 0;
|
|
|
|
|
int matchIndex = 0;
|
|
|
|
|
int matchLimit = currentImage.Height / 2;
|
|
|
|
|
|
2024-05-24 18:08:51 +12:00
|
|
|
|
ignoreSideOffset = Math.Max(ignoreSideOffset, currentImage.Width / 20);
|
|
|
|
|
ignoreSideOffset = Math.Min(ignoreSideOffset, currentImage.Width / 3);
|
2023-03-02 12:42:36 +13:00
|
|
|
|
|
2024-05-24 18:08:51 +12:00
|
|
|
|
ignoreBottomOffset = Math.Max(ignoreBottomOffset, currentImage.Height / 20);
|
|
|
|
|
ignoreBottomOffset = Math.Min(ignoreBottomOffset, currentImage.Height / 3);
|
2024-05-16 14:28:33 +12:00
|
|
|
|
|
|
|
|
|
Rectangle rect = new Rectangle(ignoreSideOffset, result.Height - currentImage.Height,
|
|
|
|
|
currentImage.Width - ignoreSideOffset * 2, currentImage.Height - ignoreBottomOffset);
|
2023-03-02 12:42:36 +13:00
|
|
|
|
|
|
|
|
|
BitmapData bdResult = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
|
|
|
|
BitmapData bdCurrentImage = currentImage.LockBits(new Rectangle(0, 0, currentImage.Width, currentImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
|
|
|
|
|
int stride = bdResult.Stride;
|
|
|
|
|
int pixelSize = stride / result.Width;
|
|
|
|
|
IntPtr resultScan0 = bdResult.Scan0 + pixelSize * ignoreSideOffset;
|
|
|
|
|
IntPtr currentImageScan0 = bdCurrentImage.Scan0 + pixelSize * ignoreSideOffset;
|
|
|
|
|
int rectBottom = rect.Bottom - 1;
|
|
|
|
|
int compareLength = pixelSize * rect.Width;
|
|
|
|
|
|
|
|
|
|
for (int currentImageY = currentImage.Height - 1; currentImageY >= 0 && matchCount < matchLimit; currentImageY--)
|
|
|
|
|
{
|
|
|
|
|
int currentMatchCount = 0;
|
|
|
|
|
|
|
|
|
|
for (int y = 0; currentImageY - y >= 0 && currentMatchCount < matchLimit; y++)
|
|
|
|
|
{
|
|
|
|
|
if (NativeMethods.memcmp(resultScan0 + ((rectBottom - y) * stride), currentImageScan0 + ((currentImageY - y) * stride), compareLength) == 0)
|
|
|
|
|
{
|
|
|
|
|
currentMatchCount++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentMatchCount > matchCount)
|
|
|
|
|
{
|
|
|
|
|
matchCount = currentMatchCount;
|
|
|
|
|
matchIndex = currentImageY;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.UnlockBits(bdResult);
|
|
|
|
|
currentImage.UnlockBits(bdCurrentImage);
|
|
|
|
|
|
2024-04-11 12:47:06 +12:00
|
|
|
|
bool bestGuess = false;
|
|
|
|
|
|
2023-03-02 12:42:36 +13:00
|
|
|
|
if (matchCount == 0 && bestMatchCount > 0)
|
|
|
|
|
{
|
|
|
|
|
matchCount = bestMatchCount;
|
|
|
|
|
matchIndex = bestMatchIndex;
|
2024-04-11 12:47:06 +12:00
|
|
|
|
bestGuess = true;
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (matchCount > 0)
|
|
|
|
|
{
|
|
|
|
|
int matchHeight = currentImage.Height - matchIndex - 1;
|
|
|
|
|
|
|
|
|
|
if (matchHeight > 0)
|
|
|
|
|
{
|
|
|
|
|
if (matchCount > bestMatchCount)
|
|
|
|
|
{
|
|
|
|
|
bestMatchCount = matchCount;
|
|
|
|
|
bestMatchIndex = matchIndex;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 14:28:33 +12:00
|
|
|
|
Bitmap newResult = new Bitmap(result.Width, result.Height - ignoreBottomOffset + matchHeight);
|
2023-03-02 12:42:36 +13:00
|
|
|
|
|
|
|
|
|
using (Graphics g = Graphics.FromImage(newResult))
|
|
|
|
|
{
|
2024-04-07 02:27:15 +13:00
|
|
|
|
g.CompositingMode = CompositingMode.SourceCopy;
|
|
|
|
|
g.InterpolationMode = InterpolationMode.NearestNeighbor;
|
|
|
|
|
|
2024-05-16 14:28:33 +12:00
|
|
|
|
g.DrawImage(result, new Rectangle(0, 0, result.Width, result.Height - ignoreBottomOffset),
|
|
|
|
|
new Rectangle(0, 0, result.Width, result.Height - ignoreBottomOffset), GraphicsUnit.Pixel);
|
|
|
|
|
g.DrawImage(currentImage, new Rectangle(0, result.Height - ignoreBottomOffset, currentImage.Width, matchHeight),
|
2023-03-02 12:42:36 +13:00
|
|
|
|
new Rectangle(0, matchIndex + 1, currentImage.Width, matchHeight), GraphicsUnit.Pixel);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 12:47:06 +12:00
|
|
|
|
if (bestGuess)
|
|
|
|
|
{
|
|
|
|
|
status = ScrollingCaptureStatus.PartiallySuccessful;
|
|
|
|
|
}
|
|
|
|
|
else if (status != ScrollingCaptureStatus.PartiallySuccessful)
|
|
|
|
|
{
|
|
|
|
|
status = ScrollingCaptureStatus.Successful;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-08 01:23:57 +12:00
|
|
|
|
return newResult;
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 12:47:06 +12:00
|
|
|
|
status = ScrollingCaptureStatus.Failed;
|
|
|
|
|
|
2024-04-08 01:23:57 +12:00
|
|
|
|
return null;
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
2024-05-24 18:08:51 +12:00
|
|
|
|
|
|
|
|
|
private int GuessBottomEdge(Bitmap img1, Bitmap img2, int ignoreSideOffset)
|
|
|
|
|
{
|
|
|
|
|
Rectangle rect = new Rectangle(ignoreSideOffset, 0, img1.Width - ignoreSideOffset * 2, img1.Height);
|
|
|
|
|
|
|
|
|
|
using (UnsafeBitmap bmp1 = new UnsafeBitmap(img1, true, ImageLockMode.ReadOnly))
|
|
|
|
|
using (UnsafeBitmap bmp2 = new UnsafeBitmap(img2, true, ImageLockMode.ReadOnly))
|
|
|
|
|
{
|
|
|
|
|
for (int y = rect.Height - 1; y >= rect.X; y--)
|
|
|
|
|
{
|
|
|
|
|
for (int x = rect.X; x < rect.Width; x++)
|
|
|
|
|
{
|
|
|
|
|
if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
|
|
|
|
|
{
|
|
|
|
|
return rect.Height - y - 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2023-03-02 12:42:36 +13:00
|
|
|
|
}
|
|
|
|
|
}
|