using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TestVideo { internal class Extraction { private List _zones; private Screen _currentScreen; private Bitmap _screenPreview; public Screen CurrentScreen { get => _currentScreen; set => _currentScreen = value; } public List Zones { get => _zones; private set => _zones = value; } public Bitmap ScreenPreview { get => _screenPreview; set => _screenPreview = value; } public Extraction(Screen screen) { CurrentScreen = screen; } public Extraction(List zones) { Zones = zones; } public void UpdateZones() { ScreenPreview = ScreenShot(); DrawZones(); foreach (Zone zone in Zones) { Bitmap bmpData = ScreenPreview.Clone(zone.ZoneArea, ScreenPreview.PixelFormat); zone.Update(bmpData); } } public void ChangeScreen(Screen newScreen) { if (newScreen != CurrentScreen) CurrentScreen = newScreen; //This makes sure that if you go from a screen with smaller dimensions only the zones that are valid for it stays List newZones = new List(); foreach (Zone zone in newZones) { if (IsZoneInBounds(zone.ZoneArea)) newZones.Add(zone); } Zones = newZones; ScreenPreview = ScreenShot(); } public List GetZonesNames() { List zoneNames = new List(); foreach (Zone zone in Zones) { zoneNames.Add(zone.Name); } return zoneNames; } public void AddZone(Rectangle zoneArea,Zone.ZoneType type,string name) { if (!IsZoneInBounds(zoneArea)) throw new ArgumentException("Rectangle is outside the screen"); Zones.Add(new Zone(zoneArea,type,name)); } public void RemoveZone(int idZone) { Zones.RemoveAt(idZone); } public Rectangle GetZoneArea(int idZone) { return Zones[idZone].ZoneArea; } public Bitmap GetZoneData(int idZone) { return Zones[idZone].ZoneData; } public string GetZoneRawText(int idZone) { return Zones[idZone].RawText; } public string[] GetZonesCleanText(int idZone) { return Zones[idZone].CleanText; } private Bitmap ScreenShot() { Bitmap screenshot; screenshot = new Bitmap(CurrentScreen.Bounds.Width, CurrentScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); // Create a graphics object from the bitmap Graphics gfxScreenshot = Graphics.FromImage(screenshot); // Take the screenshot from the upper left corner to the right bottom corner gfxScreenshot.CopyFromScreen( CurrentScreen.Bounds.X, CurrentScreen.Bounds.Y, 0, 0, CurrentScreen.Bounds.Size, CopyPixelOperation.SourceCopy); return screenshot; } public void DrawZones() { using (Graphics g = Graphics.FromImage(ScreenPreview)) { foreach (Zone zone in Zones) { g.DrawRectangle(new Pen(Color.Red,3),zone.ZoneArea); } } } private string SaveScreenshot(Bitmap image) { //This method is now quite old. If you plan to use it I would recommand rewriting it string path = ""; string directory = AppDomain.CurrentDomain.BaseDirectory + "Screens"; path = directory + "\\Screenshot" + DateTime.Now.ToString("yyyy_MM_dd-hh_mm_ss") + ".png"; if (Directory.Exists(directory)) { image.Save(path, ImageFormat.Png); } else { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "\\Screens"); } return path; } private bool IsZoneInBounds(Rectangle zoneArea) { bool result = true; //Check that the position of the zone is possible if (zoneArea.X < 0 || zoneArea.X >= CurrentScreen.Bounds.Width) result = false; if (zoneArea.Y < 0 || zoneArea.Y >= CurrentScreen.Bounds.Width) result = false; //Check that the zone does'nt go outside the bounds of the Screen if (zoneArea.X + zoneArea.Width >= CurrentScreen.Bounds.Width) result = false; if (zoneArea.Y + zoneArea.Height >= CurrentScreen.Bounds.Height) result = false; return result; } } }