Screenshooter: Allow image reusing for better memory reutilization.

FrmScreenshooter: Continuous screenshoots.
This commit is contained in:
2018-11-05 00:14:58 +01:00
parent e8a6cfb672
commit 7c457acff1
3 changed files with 73 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using VAR.Toolbox.Code.Windows;
@@ -7,7 +8,7 @@ namespace VAR.Toolbox.Code
{
public class Screenshooter
{
public static Bitmap CaptureScreen()
public static Bitmap CaptureScreen(Bitmap bmp = null)
{
// Determine the size of the "virtual screen", which includes all monitors.
int screenLeft = SystemInformation.VirtualScreen.Left;
@@ -16,7 +17,10 @@ namespace VAR.Toolbox.Code
int screenHeight = SystemInformation.VirtualScreen.Height;
// Create a bitmap of the appropriate size to receive the screenshot.
Bitmap bmp = new Bitmap(screenWidth, screenHeight);
if (bmp == null || bmp?.Width != screenWidth || bmp?.Height != screenHeight)
{
bmp = new Bitmap(screenWidth, screenHeight);
}
// Draw the screenshot into our bitmap.
using (Graphics g = Graphics.FromImage(bmp))
@@ -26,6 +30,14 @@ namespace VAR.Toolbox.Code
return bmp;
}
[DllImport("user32.dll", SetLastError = false)]
private static extern IntPtr GetDesktopWindow();
public static Image CaptureDesktop()
{
return CaptureWindow(GetDesktopWindow());
}
/// <summary>
/// Creates an Image object containing a screen shot of a specific window
/// </summary>
@@ -38,6 +50,8 @@ namespace VAR.Toolbox.Code
// get the size
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int left = windowRect.left;
int top = windowRect.top;
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// create a device context we can copy to

View File

@@ -30,6 +30,7 @@
{
this.btnScreenshoot = new System.Windows.Forms.Button();
this.picViewer = new VAR.Toolbox.Controls.CtrImageViewer();
this.btnStartStop = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.picViewer)).BeginInit();
this.SuspendLayout();
//
@@ -46,8 +47,8 @@
//
// picViewer
//
this.picViewer.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
this.picViewer.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.picViewer.BackColor = System.Drawing.Color.Black;
this.picViewer.ImageShow = null;
@@ -57,11 +58,23 @@
this.picViewer.TabIndex = 0;
this.picViewer.TabStop = false;
//
// btnStartStop
//
this.btnStartStop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnStartStop.Location = new System.Drawing.Point(93, 390);
this.btnStartStop.Name = "btnStartStop";
this.btnStartStop.Size = new System.Drawing.Size(75, 23);
this.btnStartStop.TabIndex = 2;
this.btnStartStop.Text = "Start";
this.btnStartStop.UseVisualStyleBackColor = true;
this.btnStartStop.Click += new System.EventHandler(this.btnStartStop_Click);
//
// FrmScreenshooter
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(605, 425);
this.Controls.Add(this.btnStartStop);
this.Controls.Add(this.btnScreenshoot);
this.Controls.Add(this.picViewer);
this.Name = "FrmScreenshooter";
@@ -75,5 +88,6 @@
private VAR.Toolbox.Controls.CtrImageViewer picViewer;
private System.Windows.Forms.Button btnScreenshoot;
private System.Windows.Forms.Button btnStartStop;
}
}

View File

@@ -1,4 +1,6 @@
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using VAR.Toolbox.Code;
@@ -6,14 +8,52 @@ namespace VAR.Toolbox.UI
{
public partial class FrmScreenshooter : Form
{
private bool _repetitiveScreenshots = false;
private Timer timTicker;
private Bitmap bmpScreen = null;
public FrmScreenshooter()
{
InitializeComponent();
if (components == null) { components = new Container(); }
timTicker = new Timer(components);
timTicker.Interval = 16;
timTicker.Enabled = false;
timTicker.Tick += TimTicker_Tick;
}
private void btnScreenshoot_Click(object sender, EventArgs e)
{
picViewer.ImageShow = Screenshooter.CaptureScreen();
bmpScreen = Screenshooter.CaptureScreen(bmpScreen);
picViewer.ImageShow = bmpScreen;
}
private void TimTicker_Tick(object sender, EventArgs e)
{
timTicker.Stop();
bmpScreen = Screenshooter.CaptureScreen(bmpScreen);
picViewer.ImageShow = bmpScreen;
timTicker.Start();
}
private void btnStartStop_Click(object sender, EventArgs e)
{
GC.Collect();
if (_repetitiveScreenshots)
{
_repetitiveScreenshots = false;
btnStartStop.Text = "Start";
timTicker.Stop();
timTicker.Enabled = false;
}
else
{
_repetitiveScreenshots = true;
btnStartStop.Text = "Stop";
timTicker.Enabled = true;
timTicker.Start();
}
}
}
}