ScrollingCaptureLightForm improvements

This commit is contained in:
Jaex 2023-03-01 23:01:00 +03:00
parent f2e412e46f
commit 43416ecf09
5 changed files with 52 additions and 36 deletions

View file

@ -923,26 +923,25 @@ public static Bitmap CreateCheckerPattern(int width, int height, Color checkerCo
return bmp;
}
public static bool IsImagesEqual(Bitmap bmp1, Bitmap bmp2)
public static bool CompareImages(Bitmap bmp1, Bitmap bmp2)
{
if ((bmp1 == null) != (bmp2 == null)) return false;
if (bmp1.Size != bmp2.Size) return false;
BitmapData bd1 = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData bd2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
try
if (bmp1 != null && bmp2 != null && bmp1.Width == bmp2.Width && bmp1.Height == bmp2.Height)
{
int stride = bd1.Stride;
int count = stride * bmp1.Height;
BitmapData bd1 = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData bd2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
return NativeMethods.memcmp(bd1.Scan0, bd2.Scan0, count) == 0;
}
finally
{
bmp1.UnlockBits(bd1);
bmp2.UnlockBits(bd2);
try
{
return NativeMethods.memcmp(bd1.Scan0, bd2.Scan0, bd1.Stride * bmp1.Height) == 0;
}
finally
{
bmp1.UnlockBits(bd1);
bmp2.UnlockBits(bd2);
}
}
return false;
}
public static bool IsImageTransparent(Bitmap bmp)

View file

@ -271,7 +271,7 @@ private void RemoveDuplicates()
{
for (int i = images.Count - 1; i > 0; i--)
{
bool result = ImageHelpers.IsImagesEqual(images[i], images[i - 1]);
bool result = ImageHelpers.CompareImages(images[i], images[i - 1]);
if (result)
{
@ -425,7 +425,7 @@ private bool IsLastTwoImagesSame()
if (images.Count > 1)
{
result = ImageHelpers.IsImagesEqual(images[images.Count - 1], images[images.Count - 2]);
result = ImageHelpers.CompareImages(images[images.Count - 1], images[images.Count - 2]);
if (result)
{

View file

@ -94,6 +94,8 @@ private void InitializeComponent()
this.Name = "ScrollingCaptureLightForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ShareX - Scrolling capture";
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.Activated += new System.EventHandler(this.ScrollingCaptureLightForm_Activated);
this.Load += new System.EventHandler(this.ScrollingCaptureLightForm_Load);
this.pOutput.ResumeLayout(false);
this.pOutput.PerformLayout();

View file

@ -26,6 +26,7 @@
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
@ -109,8 +110,7 @@ private async Task StartCapture()
bestMatchCount = 0;
bestMatchIndex = 0;
// TODO: Translate
btnCapture.Text = "Stop";
btnCapture.Enabled = false;
WindowState = FormWindowState.Minimized;
Reset();
selectedWindow.Activate();
@ -122,18 +122,21 @@ private async Task StartCapture()
private void StopCapture()
{
stopRequested = true;
if (isCapturing)
{
stopRequested = true;
}
}
private void EndCapture()
{
// TODO: Translate
btnCapture.Text = "Capture...";
btnCapture.Enabled = true;
btnUpload.Enabled = Result != null;
pbOutput.Image = Result;
this.ForceActivate();
isCapturing = false;
this.ForceActivate();
}
private async Task CaptureJob()
@ -141,9 +144,21 @@ private async Task CaptureJob()
InputHelpers.SendKeyPress(VirtualKeyCode.HOME);
NativeMethods.SendMessage(selectedWindow.Handle, (int)WindowsMessages.VSCROLL, (int)ScrollBarCommands.SB_TOP, 0);
Stopwatch timer = new Stopwatch();
do
{
await Task.Delay(Options.ScrollDelay);
int delay = Options.ScrollDelay - (int)timer.ElapsedMilliseconds;
if (delay > 0)
{
await Task.Delay(delay);
}
if (stopRequested)
{
break;
}
Screenshot screenshot = new Screenshot()
{
@ -160,6 +175,8 @@ private async Task CaptureJob()
InputHelpers.SendMouseWheel(-120 * 2);
currentScrollCount++;
timer.Restart();
if (images.Count > 0)
{
Result = await CombineImagesAsync(Result, images[images.Count - 1]);
@ -190,7 +207,7 @@ private bool IsLastTwoImagesSame()
if (images.Count > 1)
{
result = ImageHelpers.IsImagesEqual(images[images.Count - 1], images[images.Count - 2]);
result = ImageHelpers.CompareImages(images[images.Count - 1], images[images.Count - 2]);
if (result)
{
@ -232,7 +249,7 @@ private Bitmap CombineImages(Bitmap result, Bitmap currentImage)
int matchCount = 0;
int matchIndex = 0;
int matchLimit = currentImage.Height / 3;
int matchLimit = currentImage.Height / 2;
int ignoreSideOffset = Math.Max(50, currentImage.Width / 20);
if (currentImage.Width < ignoreSideOffset * 3)
@ -331,16 +348,14 @@ private async void ScrollingCaptureLightForm_Load(object sender, EventArgs e)
await SelectWindow();
}
private void ScrollingCaptureLightForm_Activated(object sender, EventArgs e)
{
StopCapture();
}
private async void btnCapture_Click(object sender, EventArgs e)
{
if (isCapturing)
{
StopCapture();
}
else
{
await SelectWindow();
}
await SelectWindow();
}
private void btnUpload_Click(object sender, EventArgs e)

View file

@ -110,7 +110,7 @@ public Bitmap CaptureWindowTransparent(IntPtr handle)
Bitmap transparentImage;
if (ImageHelpers.IsImagesEqual(whiteBackground, whiteBackground2))
if (ImageHelpers.CompareImages(whiteBackground, whiteBackground2))
{
transparentImage = CreateTransparentImage(whiteBackground, blackBackground);
isTransparent = true;