using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Drawing; namespace OCR_tester { public class Zone { protected Bitmap FullImage; private List _zones; private List _windows; protected Rectangle _bounds; public List Zones { get => _zones; protected set => _zones = value; } public List Windows { get => _windows; protected set => _windows = value; } public Rectangle Bounds { get => _bounds; private set => _bounds = value; } public Bitmap ZoneImage { get { Bitmap sample = new Bitmap(Bounds.Width, Bounds.Height); Graphics g = Graphics.FromImage(sample); g.DrawImage(FullImage, new Rectangle(0, 0, sample.Width, sample.Height), Bounds, GraphicsUnit.Pixel); return sample; } } public Zone(Image fullImage, Rectangle bounds) { FullImage = (Bitmap)fullImage; Init(bounds); } public Zone(Bitmap fullImage, Rectangle bounds) { FullImage = fullImage; Init(bounds); } private void Init(Rectangle bounds) { Bounds = bounds; Zones = new List(); Windows = new List(); } public void AddZone(Rectangle bounds) { if (Fits(bounds)) Zones.Add(new Zone(ZoneImage, bounds)); } public void AddWindow(Rectangle bounds) { if (Fits(bounds)) Windows.Add(new Window(ZoneImage, bounds)); } /// /// Checks if the given Rectangle fits in the current zone /// /// The Rectangle you want to check the fittment /// protected bool Fits(Rectangle inputRectangle) { if (inputRectangle.X + inputRectangle.Width > Bounds.Width || inputRectangle.Y + inputRectangle.Height > Bounds.Height || inputRectangle.X < 0 || inputRectangle.Y < 0) { return false; } else { return true; } } public virtual Bitmap Draw() { Image img = ZoneImage; Graphics g = Graphics.FromImage(img); foreach (Window w in Windows) { g.DrawRectangle(Pens.Blue, w.Bounds); } return (Bitmap)img; } } }