From ae677d49500b13a2776202788e62ebb1c739430c Mon Sep 17 00:00:00 2001 From: maxluli Date: Thu, 20 Oct 2022 13:50:54 +0200 Subject: [PATCH] Tried to fix the offset between clicks and the actual zone but its still not perfect. And you can delete the zones --- TestVideo/Extraction.cs | 21 ++++++++++++----- TestVideo/Form1.Designer.cs | 21 +++++++++-------- TestVideo/Form1.cs | 47 ++++++++++++++++++++++++++++++++++--- 3 files changed, 70 insertions(+), 19 deletions(-) diff --git a/TestVideo/Extraction.cs b/TestVideo/Extraction.cs index 6cd4758..37470dc 100644 --- a/TestVideo/Extraction.cs +++ b/TestVideo/Extraction.cs @@ -73,26 +73,35 @@ namespace TestVideo } public void RemoveZone(int idZone) { - Zones.RemoveAt(idZone); + if (Zones.Count > idZone) + Zones.RemoveAt(idZone); } public Rectangle GetZoneArea(int idZone) { - return Zones[idZone].ZoneArea; + if (Zones.Count > idZone) + return Zones[idZone].ZoneArea; + return new Rectangle(new Point(0,0),new Size(0, 0)); } public Bitmap GetZoneData(int idZone) { - return Zones[idZone].ZoneData; + if (Zones.Count > idZone) + return Zones[idZone].ZoneData; + return new Bitmap(1,1); } public string GetZoneRawText(int idZone) { - return Zones[idZone].RawText; + if(Zones.Count > idZone) + return Zones[idZone].RawText; + return ""; } public string[] GetZonesCleanText(int idZone) { - return Zones[idZone].CleanText; + if (Zones.Count > idZone) + return Zones[idZone].CleanText; + return new string[] { "" }; } private Bitmap ScreenShot() { @@ -116,7 +125,7 @@ namespace TestVideo { foreach (Zone zone in Zones) { - g.DrawRectangle(new Pen(Color.Red,3),zone.ZoneArea); + g.DrawRectangle(new Pen(Color.Green,3),zone.ZoneArea); } } } diff --git a/TestVideo/Form1.Designer.cs b/TestVideo/Form1.Designer.cs index 5f9e7f6..468d391 100644 --- a/TestVideo/Form1.Designer.cs +++ b/TestVideo/Form1.Designer.cs @@ -37,7 +37,7 @@ this.tbxResult = new System.Windows.Forms.TextBox(); this.lsbZones = new System.Windows.Forms.ListBox(); this.btnCreateZone = new System.Windows.Forms.Button(); - this.button2 = new System.Windows.Forms.Button(); + this.btnDeleteZone = new System.Windows.Forms.Button(); this.lsbZoneTypes = new System.Windows.Forms.ListBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); @@ -121,14 +121,15 @@ this.btnCreateZone.UseVisualStyleBackColor = true; this.btnCreateZone.Click += new System.EventHandler(this.btnCreateZone_Click); // - // button2 + // btnDeleteZone // - this.button2.Location = new System.Drawing.Point(978, 453); - this.button2.Name = "button2"; - this.button2.Size = new System.Drawing.Size(482, 37); - this.button2.TabIndex = 7; - this.button2.Text = "Delete Zone"; - this.button2.UseVisualStyleBackColor = true; + this.btnDeleteZone.Location = new System.Drawing.Point(978, 453); + this.btnDeleteZone.Name = "btnDeleteZone"; + this.btnDeleteZone.Size = new System.Drawing.Size(482, 37); + this.btnDeleteZone.TabIndex = 7; + this.btnDeleteZone.Text = "Delete Zone"; + this.btnDeleteZone.UseVisualStyleBackColor = true; + this.btnDeleteZone.Click += new System.EventHandler(this.btnDeleteZone_Click); // // lsbZoneTypes // @@ -195,7 +196,7 @@ this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.lsbZoneTypes); - this.Controls.Add(this.button2); + this.Controls.Add(this.btnDeleteZone); this.Controls.Add(this.btnCreateZone); this.Controls.Add(this.lsbZones); this.Controls.Add(this.tbxResult); @@ -222,7 +223,7 @@ private System.Windows.Forms.TextBox tbxResult; private System.Windows.Forms.ListBox lsbZones; private System.Windows.Forms.Button btnCreateZone; - private System.Windows.Forms.Button button2; + private System.Windows.Forms.Button btnDeleteZone; private System.Windows.Forms.ListBox lsbZoneTypes; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; diff --git a/TestVideo/Form1.cs b/TestVideo/Form1.cs index f5e3464..ffb94a3 100644 --- a/TestVideo/Form1.cs +++ b/TestVideo/Form1.cs @@ -12,6 +12,7 @@ using Emgu.CV; using Emgu.CV.OCR; using Emgu.CV.Structure; using System.IO; +using System.Runtime.InteropServices; namespace TestVideo { @@ -29,6 +30,8 @@ namespace TestVideo public Form1() { InitializeComponent(); + SetDpiAwareness(); + screens = Screen.AllScreens; extraction = new Extraction(screens[0]); currentZoneIndex = -1; @@ -97,10 +100,13 @@ namespace TestVideo if (creatingZone) { //Convert coordinates to image coordinates - Rectangle screenSize = extraction.CurrentScreen.Bounds; - float xRatio = (float)screenSize.Width / (float)pbxInput.Width; - float yRatio = (float)screenSize.Height / (float)pbxInput.Height; + + //Rectangle screenSize = extraction.CurrentScreen.Bounds; + float xRatio = (float)pbxInput.Image.Width / (float)pbxInput.Width; + float yRatio = (float)pbxInput.Image.Height / (float)pbxInput.Height; Point tmpPoint = pbxInput.PointToClient(MousePosition); + //MouseEventArgs me = (MouseEventArgs)e; + //Point tmpPoint = me.Location; Point newPoint = new Point(Convert.ToInt32((float)tmpPoint.X * xRatio),Convert.ToInt32((float)tmpPoint.Y * yRatio)); if (newZonePoints[0] == new Point(-1,-1)) @@ -148,5 +154,40 @@ namespace TestVideo currentZoneIndex = lsbZones.SelectedIndex; } } + + private void btnDeleteZone_Click(object sender, EventArgs e) + { + if (lsbZones.SelectedIndex != -1) + extraction.RemoveZone(lsbZones.SelectedIndex); + RefreshUi(); + } + + //ProcessDPIAwarness, SetProcessDpiAwarness and SetDpi Awarness are not coded by me + //They have been found on stack overflow https://stackoverflow.com/questions/27987085/screen-resolution-not-matching-screen-bounds + //To fix a problem where Screen.Bounds returned false values + //You use it just by callinf SetDpiAwarness() before calling Screen.Bounds + private enum ProcessDPIAwareness + { + ProcessDPIUnaware = 0, + ProcessSystemDPIAware = 1, + ProcessPerMonitorDPIAware = 2 + } + + [DllImport("shcore.dll")] + private static extern int SetProcessDpiAwareness(ProcessDPIAwareness value); + + private static void SetDpiAwareness() + { + try + { + if (Environment.OSVersion.Version.Major >= 6) + { + SetProcessDpiAwareness(ProcessDPIAwareness.ProcessPerMonitorDPIAware); + } + } + catch (EntryPointNotFoundException)//this exception occures if OS does not implement this API, just ignore it. + { + } + } } }