166 lines
5.5 KiB
C#
166 lines
5.5 KiB
C#
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<Zone> _zones;
|
|
private Screen _currentScreen;
|
|
private Bitmap _screenPreview;
|
|
|
|
public Screen CurrentScreen { get => _currentScreen; set => _currentScreen = value; }
|
|
public List<Zone> Zones { get => _zones; private set => _zones = value; }
|
|
public Bitmap ScreenPreview { get => _screenPreview; set => _screenPreview = value; }
|
|
|
|
public Extraction(Screen screen)
|
|
{
|
|
CurrentScreen = screen;
|
|
}
|
|
public Extraction(List<Zone> 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<Zone> newZones = new List<Zone>();
|
|
foreach (Zone zone in newZones)
|
|
{
|
|
if (IsZoneInBounds(zone.ZoneArea))
|
|
newZones.Add(zone);
|
|
}
|
|
Zones = newZones;
|
|
|
|
ScreenPreview = ScreenShot();
|
|
}
|
|
public List<string> GetZonesNames()
|
|
{
|
|
List<string> zoneNames = new List<string>();
|
|
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)
|
|
{
|
|
if (Zones.Count > idZone)
|
|
Zones.RemoveAt(idZone);
|
|
}
|
|
|
|
public Rectangle GetZoneArea(int idZone)
|
|
{
|
|
if (Zones.Count > idZone)
|
|
return Zones[idZone].ZoneArea;
|
|
return new Rectangle(new Point(0,0),new Size(0, 0));
|
|
}
|
|
|
|
public Bitmap GetZoneData(int idZone)
|
|
{
|
|
if (Zones.Count > idZone)
|
|
return Zones[idZone].ZoneData;
|
|
return new Bitmap(1,1);
|
|
}
|
|
|
|
public string GetZoneRawText(int idZone)
|
|
{
|
|
if(Zones.Count > idZone)
|
|
return Zones[idZone].RawText;
|
|
return "";
|
|
}
|
|
public string[] GetZonesCleanText(int idZone)
|
|
{
|
|
if (Zones.Count > idZone)
|
|
return Zones[idZone].CleanText;
|
|
return new string[] { "" };
|
|
}
|
|
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.Green,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;
|
|
}
|
|
}
|
|
}
|