using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.OCR; using Emgu.CV.Structure; using System.IO; using System.Runtime.InteropServices; namespace TestVideo { public partial class Form1 : Form { public Screen[] screens; List screenNames; Extraction extraction; int currentZoneIndex; List zoneTypes; bool creatingZone; Point[] newZonePoints; public Form1() { InitializeComponent(); SetDpiAwareness(); screens = Screen.AllScreens; extraction = new Extraction(screens[0]); currentZoneIndex = -1; screenNames = new List(); foreach (Screen scr in screens) { screenNames.Add(scr.DeviceName); } lsbScreens.DataSource = screenNames; zoneTypes = new List(); foreach (object type in Enum.GetValues(typeof(Zone.ZoneType))) { zoneTypes.Add((Zone.ZoneType)type); } lsbZoneTypes.DataSource = zoneTypes; newZonePoints = new Point[2] {new Point(-1,-1), new Point(-1,-1)}; } private void btnStart_Click(object sender, EventArgs e) { if (tmrScreenshots.Enabled) { tmrScreenshots.Enabled = false; btnStart.Text = "Start recording"; } else { tmrScreenshots.Enabled = true; btnStart.Text = "Stop recording"; } } public void RefreshUi() { lsbZones.DataSource = extraction.GetZonesNames(); extraction.DrawZones(); //extraction.UpdateZones(); pbxInput.Image = extraction.ScreenPreview; } private void tmrScreenshots_Tick(object sender, EventArgs e) { extraction.UpdateZones(); pbxInput.Image = extraction.ScreenPreview; if(currentZoneIndex != -1) { tbxResult.Text = extraction.GetZoneRawText(currentZoneIndex); pbxOutput.Image = extraction.GetZoneData(currentZoneIndex); } //This is to prevent Ram from exploding GC.Collect(); } private void lsbScreens_SelectedIndexChanged(object sender, EventArgs e) { extraction.ChangeScreen(screens[lsbScreens.SelectedIndex]); pbxInput.Image = extraction.ScreenPreview; } private void pbxInput_Click(object sender, EventArgs e) { if (creatingZone) { //Convert coordinates to image coordinates //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)) { //Its the first point newZonePoints[0] = newPoint; btnCreateZone.Text = "Creating Zone 1/2 points selected"; } else { //Its the second point newZonePoints[1] = newPoint; btnCreateZone.Text = "Create Zone"; Zone.ZoneType newZoneType = zoneTypes[lsbZoneTypes.SelectedIndex]; Random rnd = new Random(); Point p1 = newZonePoints[0]; Point p2 = newZonePoints[1]; Size newSize = new Size(Math.Max(p1.X,p2.X) - Math.Min(p1.X, p2.X),Math.Max(p1.Y,p2.Y) - Math.Min(p1.Y, p2.Y)); extraction.AddZone(new Rectangle(newZonePoints[0],newSize),newZoneType,"Coucou"); newZonePoints = new Point[2] { new Point(-1, -1), new Point(-1, -1) }; creatingZone = false; RefreshUi(); } } } private void btnCreateZone_Click(object sender, EventArgs e) { creatingZone = !creatingZone; if (creatingZone) { btnCreateZone.Text = "Creating Zone 0/2 points selected"; } else { btnCreateZone.Text = "Create Zone"; } } private void lsbZones_SelectedIndexChanged(object sender, EventArgs e) { if (extraction.GetZonesNames().Any() && lsbZones.SelectedIndex >= 0) { 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. { } } } }