From 7c457acff1178ac3bd8ddfad11553d85707789a6 Mon Sep 17 00:00:00 2001 From: "Valeriano A.R" Date: Mon, 5 Nov 2018 00:14:58 +0100 Subject: [PATCH] Screenshooter: Allow image reusing for better memory reutilization. FrmScreenshooter: Continuous screenshoots. --- VAR.Toolbox/Code/Screenshooter.cs | 18 ++++++++- VAR.Toolbox/UI/FrmScreenshooter.Designer.cs | 18 ++++++++- VAR.Toolbox/UI/FrmScreenshooter.cs | 42 ++++++++++++++++++++- 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/VAR.Toolbox/Code/Screenshooter.cs b/VAR.Toolbox/Code/Screenshooter.cs index 1937ed4..2395fa5 100644 --- a/VAR.Toolbox/Code/Screenshooter.cs +++ b/VAR.Toolbox/Code/Screenshooter.cs @@ -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()); + } + /// /// Creates an Image object containing a screen shot of a specific window /// @@ -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 diff --git a/VAR.Toolbox/UI/FrmScreenshooter.Designer.cs b/VAR.Toolbox/UI/FrmScreenshooter.Designer.cs index 30e880f..8b5051e 100644 --- a/VAR.Toolbox/UI/FrmScreenshooter.Designer.cs +++ b/VAR.Toolbox/UI/FrmScreenshooter.Designer.cs @@ -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; } } \ No newline at end of file diff --git a/VAR.Toolbox/UI/FrmScreenshooter.cs b/VAR.Toolbox/UI/FrmScreenshooter.cs index 41a9f6f..17dbe03 100644 --- a/VAR.Toolbox/UI/FrmScreenshooter.cs +++ b/VAR.Toolbox/UI/FrmScreenshooter.cs @@ -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(); + } } } }