Files
OCR_TEST/OCR_tester/Window.cs
T

65 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
namespace OCR_tester
{
public class Window
{
protected Bitmap FullImage;
private string _name;
private Rectangle _bounds;
public string Name { get => _name; protected set => _name = value; }
public Rectangle Bounds { get => _bounds; protected set => _bounds = value; }
public static DirectoryInfo tessDataFolder = new DirectoryInfo(@"C:\Users\Moi\Pictures\SeleniumScreens\TessData");
public Bitmap WindowImage
{
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 Window(Bitmap fullImage, Rectangle bounds)
{
FullImage = fullImage;
Bounds = bounds;
}
public virtual void RecoverInformations()
{
//Each Window type will have to implement its own way to recover the informations stored in the Window Image
}
public static byte[] ImageToByte(Image img)
{
using (var stream = new MemoryStream())
{
img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return stream.ToArray();
}
}
public virtual string ToJSON()
{
string result = "";
result += "\"" + Name + "\"" + ":{" + Environment.NewLine;
result += "\t" + "\"x\":" + Bounds.X + "," + Environment.NewLine;
result += "\t" + "\"y\":" + Bounds.Y + "," + Environment.NewLine;
result += "\t" + "\"width\":" + Bounds.Width + Environment.NewLine;
result += "}";
return result;
}
}
}