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; 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 != null && bmp2 != null && bmp1.Width == bmp2.Width && bmp1.Height == bmp2.Height)
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
{ {
int stride = bd1.Stride; BitmapData bd1 = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
int count = stride * bmp1.Height; 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; try
} {
finally return NativeMethods.memcmp(bd1.Scan0, bd2.Scan0, bd1.Stride * bmp1.Height) == 0;
{ }
bmp1.UnlockBits(bd1); finally
bmp2.UnlockBits(bd2); {
bmp1.UnlockBits(bd1);
bmp2.UnlockBits(bd2);
}
} }
return false;
} }
public static bool IsImageTransparent(Bitmap bmp) public static bool IsImageTransparent(Bitmap bmp)

View file

@ -271,7 +271,7 @@ private void RemoveDuplicates()
{ {
for (int i = images.Count - 1; i > 0; i--) 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) if (result)
{ {
@ -425,7 +425,7 @@ private bool IsLastTwoImagesSame()
if (images.Count > 1) 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) if (result)
{ {

View file

@ -94,6 +94,8 @@ private void InitializeComponent()
this.Name = "ScrollingCaptureLightForm"; this.Name = "ScrollingCaptureLightForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "ShareX - Scrolling capture"; 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.Load += new System.EventHandler(this.ScrollingCaptureLightForm_Load);
this.pOutput.ResumeLayout(false); this.pOutput.ResumeLayout(false);
this.pOutput.PerformLayout(); this.pOutput.PerformLayout();

View file

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

View file

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