2023-02-26 04:42:21 +13:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
|
|
|
|
Copyright (c) 2007-2023 ShareX Team
|
|
|
|
|
|
|
|
|
|
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.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Threading;
|
2023-02-26 06:00:08 +13:00
|
|
|
|
using System.Threading.Tasks;
|
2023-02-26 04:42:21 +13:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace ShareX.ScreenCaptureLib
|
|
|
|
|
{
|
|
|
|
|
public partial class ScrollingCaptureLightForm : Form
|
|
|
|
|
{
|
|
|
|
|
public ScrollingCaptureOptions Options { get; private set; }
|
|
|
|
|
public Bitmap Result { get; private set; }
|
|
|
|
|
|
|
|
|
|
private List<Bitmap> images = new List<Bitmap>();
|
2023-02-26 06:00:08 +13:00
|
|
|
|
private bool isCapturing, scrollTop;
|
2023-02-26 04:42:21 +13:00
|
|
|
|
private int currentScrollCount;
|
|
|
|
|
private WindowInfo selectedWindow;
|
|
|
|
|
private Rectangle selectedRectangle;
|
|
|
|
|
|
|
|
|
|
public ScrollingCaptureLightForm(ScrollingCaptureOptions options)
|
|
|
|
|
{
|
|
|
|
|
Options = options;
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
ShareXResources.ApplyTheme(this);
|
|
|
|
|
|
|
|
|
|
SelectWindow();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Reset()
|
|
|
|
|
{
|
|
|
|
|
currentScrollCount = 0;
|
|
|
|
|
|
|
|
|
|
ResetPictureBox();
|
|
|
|
|
|
|
|
|
|
if (images != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (Bitmap bmp in images)
|
|
|
|
|
{
|
|
|
|
|
bmp?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
images.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ResetPictureBox()
|
|
|
|
|
{
|
|
|
|
|
Image temp = pbOutput.Image;
|
|
|
|
|
pbOutput.Image = null;
|
|
|
|
|
temp?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void StartCapture()
|
|
|
|
|
{
|
|
|
|
|
if (!isCapturing)
|
|
|
|
|
{
|
|
|
|
|
isCapturing = true;
|
2023-02-26 06:00:08 +13:00
|
|
|
|
scrollTop = true;
|
|
|
|
|
// TODO: Translate
|
|
|
|
|
btnCapture.Text = "Stop";
|
2023-02-26 04:42:21 +13:00
|
|
|
|
WindowState = FormWindowState.Minimized;
|
|
|
|
|
Reset();
|
|
|
|
|
selectedWindow.Activate();
|
|
|
|
|
|
|
|
|
|
tCapture.Interval = Options.StartDelay;
|
|
|
|
|
tCapture.Start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 06:00:08 +13:00
|
|
|
|
private async Task StopCapture()
|
2023-02-26 04:42:21 +13:00
|
|
|
|
{
|
|
|
|
|
if (isCapturing)
|
|
|
|
|
{
|
|
|
|
|
tCapture.Stop();
|
2023-02-26 06:00:08 +13:00
|
|
|
|
// TODO: Translate
|
|
|
|
|
btnCapture.Text = "Capture...";
|
|
|
|
|
btnCapture.Enabled = false;
|
2023-02-26 04:42:21 +13:00
|
|
|
|
this.ForceActivate();
|
|
|
|
|
|
2023-02-26 06:00:08 +13:00
|
|
|
|
Result = await CombineImagesAsync(images);
|
2023-02-26 04:42:21 +13:00
|
|
|
|
pbOutput.Image = Result;
|
|
|
|
|
|
2023-02-26 06:00:08 +13:00
|
|
|
|
btnCapture.Enabled = true;
|
2023-02-26 04:42:21 +13:00
|
|
|
|
isCapturing = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return IsLastTwoImagesSame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsLastTwoImagesSame()
|
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
|
|
if (images.Count > 1)
|
|
|
|
|
{
|
|
|
|
|
result = ImageHelpers.IsImagesEqual(images[images.Count - 1], images[images.Count - 2]);
|
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
Bitmap last = images[images.Count - 1];
|
|
|
|
|
images.Remove(last);
|
|
|
|
|
last.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SelectWindow()
|
|
|
|
|
{
|
|
|
|
|
WindowState = FormWindowState.Minimized;
|
|
|
|
|
Thread.Sleep(250);
|
|
|
|
|
|
2023-02-26 06:00:08 +13:00
|
|
|
|
if (RegionCaptureTasks.GetRectangleRegion(out selectedRectangle, out selectedWindow, new RegionCaptureOptions()))
|
2023-02-26 04:42:21 +13:00
|
|
|
|
{
|
|
|
|
|
StartCapture();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
this.ForceActivate();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 06:00:08 +13:00
|
|
|
|
private async Task<Bitmap> CombineImagesAsync(List<Bitmap> images)
|
|
|
|
|
{
|
|
|
|
|
return await Task.Run(() => CombineImages(images));
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 04:42:21 +13:00
|
|
|
|
private Bitmap CombineImages(List<Bitmap> images)
|
|
|
|
|
{
|
|
|
|
|
Bitmap result = (Bitmap)images[0].Clone();
|
|
|
|
|
|
|
|
|
|
int bestMatchCount = 0;
|
|
|
|
|
int bestMatchIndex = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < images.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
Bitmap currentImage = images[i];
|
|
|
|
|
|
|
|
|
|
int matchCount = 0;
|
|
|
|
|
int matchIndex = 0;
|
|
|
|
|
int matchLimit = currentImage.Height / 3;
|
|
|
|
|
int ignoreSideOffset = Math.Max(50, currentImage.Width / 20);
|
|
|
|
|
|
|
|
|
|
if (currentImage.Width < ignoreSideOffset * 3)
|
|
|
|
|
{
|
|
|
|
|
ignoreSideOffset = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Rectangle rect = new Rectangle(ignoreSideOffset, result.Height - currentImage.Height, currentImage.Width - ignoreSideOffset * 2, currentImage.Height);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (matchCount == 0 && bestMatchCount > 0)
|
|
|
|
|
{
|
|
|
|
|
matchCount = bestMatchCount;
|
|
|
|
|
matchIndex = bestMatchIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (matchCount > 0)
|
|
|
|
|
{
|
|
|
|
|
if (matchCount > bestMatchCount)
|
|
|
|
|
{
|
|
|
|
|
bestMatchCount = matchCount;
|
|
|
|
|
bestMatchIndex = matchIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Bitmap newResult = new Bitmap(result.Width, result.Height + currentImage.Height - matchIndex - 1);
|
|
|
|
|
|
|
|
|
|
using (Graphics g = Graphics.FromImage(newResult))
|
|
|
|
|
{
|
|
|
|
|
g.DrawImage(result, new Rectangle(0, 0, result.Width, result.Height),
|
|
|
|
|
new Rectangle(0, 0, result.Width, result.Height), GraphicsUnit.Pixel);
|
|
|
|
|
g.DrawImage(currentImage, new Rectangle(0, result.Height, currentImage.Width, currentImage.Height - matchIndex - 1),
|
|
|
|
|
new Rectangle(0, matchIndex + 1, currentImage.Width, currentImage.Height - matchIndex - 1), GraphicsUnit.Pixel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.Dispose();
|
|
|
|
|
result = newResult;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 06:00:08 +13:00
|
|
|
|
private async void btnCapture_Click(object sender, EventArgs e)
|
2023-02-26 04:42:21 +13:00
|
|
|
|
{
|
|
|
|
|
if (isCapturing)
|
|
|
|
|
{
|
2023-02-26 06:00:08 +13:00
|
|
|
|
await StopCapture();
|
2023-02-26 04:42:21 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SelectWindow();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void btnOptions_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-26 06:00:08 +13:00
|
|
|
|
private async void tCapture_Tick(object sender, EventArgs e)
|
2023-02-26 04:42:21 +13:00
|
|
|
|
{
|
2023-02-26 06:00:08 +13:00
|
|
|
|
if (scrollTop)
|
2023-02-26 04:42:21 +13:00
|
|
|
|
{
|
2023-02-26 06:00:08 +13:00
|
|
|
|
scrollTop = false;
|
2023-02-26 04:42:21 +13:00
|
|
|
|
tCapture.Interval = Options.ScrollDelay;
|
|
|
|
|
|
|
|
|
|
InputHelpers.SendKeyPress(VirtualKeyCode.HOME);
|
|
|
|
|
NativeMethods.SendMessage(selectedWindow.Handle, (int)WindowsMessages.VSCROLL, (int)ScrollBarCommands.SB_TOP, 0);
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Screenshot screenshot = new Screenshot() { CaptureCursor = false };
|
|
|
|
|
Bitmap bmp = screenshot.CaptureRectangle(selectedRectangle);
|
|
|
|
|
|
|
|
|
|
if (bmp != null)
|
|
|
|
|
{
|
|
|
|
|
images.Add(bmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentScrollCount == Options.MaximumScrollCount || (Options.AutoDetectScrollEnd && IsScrollReachedBottom(selectedWindow.Handle)))
|
|
|
|
|
{
|
2023-02-26 06:00:08 +13:00
|
|
|
|
await StopCapture();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
InputHelpers.SendMouseWheel(-120 * 2);
|
|
|
|
|
currentScrollCount++;
|
2023-02-26 04:42:21 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|